libxcoder  5.0.1
ni_device_api_priv.c
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (C) 2022 NETINT Technologies
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  * SOFTWARE.
19  *
20  ******************************************************************************/
21 
22 /*!*****************************************************************************
23  * \file ni_device_api_priv.c
24  *
25  * \brief Private definitions used by ni_device_api.c for video processing
26  * tasks
27  ******************************************************************************/
28 
29 #ifdef _WIN32
30 #include <windows.h>
31 #elif __linux__ || __APPLE__
32 #if __linux__
33 #include <linux/types.h>
34 #include <malloc.h>
35 #include <sys/prctl.h>
36 #endif
37 #include <sys/file.h>
38 #include <sys/ioctl.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <sys/mman.h>
42 #include <sys/types.h>
43 #include <utime.h>
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <semaphore.h>
48 #include <limits.h>
49 #include <signal.h>
50 #include <dirent.h>
51 #include <sched.h>
52 #include <sys/syscall.h>
53 #include <sys/resource.h>
54 #ifndef _ANDROID
55 #include <execinfo.h>
56 #endif
57 #endif
58 
59 #include <stdint.h>
60 #include <string.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <math.h>
64 #ifdef __linux__
65 #include <stdint.h>
66 #include <poll.h>
67 #include "ni_p2p_ioctl.h"
68 #endif
69 #include "inttypes.h"
70 #include "ni_nvme.h"
71 #include "ni_device_api.h"
72 #include "ni_device_api_priv.h"
73 #include "ni_util.h"
74 #include "ni_lat_meas.h"
75 #include "ni_rsrc_priv.h"
76 
77 //Map the gopPresetIdx to gopSize,
78 //gopPresetIdx -1: Adaptive Gop, using the biggest gopsize 8
79 //gopPresetIdx 0 : Customer Gop
80 //g_map_preset_to_gopsize[lookAheadEnable][gopPresetIdx + 1];
81 //for the not support gopPresetIdx value, such as gopPresetIdx == 2, set g_map_preset_to_gopsize to -1
82 static const int g_map_preset_to_gopsize[2][18] = {{8, 0, 1, -1, 1, 2, 4, -1, 4, 8, 1, 4, -1, -1, -1, -1, 16},
83  {8, 0, -1, -1, -1, 2, 4, -1, -1, 8, 1, -1, -1, -1, -1, -1, -1}};
84 #define MAGIC_P2P_VALUE "p2p"
85 
87 {
91 
92 typedef enum
93 {
94  OPT_1 = 1, // is a combination of OPT_3 and OPT_2 in that order
95  OPT_2 = 2, // returns NI_RETCODE_FAILURE after NI_XCODER_FAILURES_MAX attempts, and LRETURNs for failures
96  OPT_3 = 3 // queries session statistics
98 
99 static uint8_t g_itu_t_t35_cc_sei_hdr_hevc[NI_CC_SEI_HDR_HEVC_LEN] = {
100  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
101  0x4e,
102  0x01, // nal_unit_header() {forbidden bit=0 nal_unit_type=39,
103  // nuh_layer_id=0 nuh_temporal_id_plus1=1)
104  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
105  0 + 11, // payLoadSize= ui16Len + 11; to be set (index 7)
106  0xb5, // itu_t_t35_country_code =181 (North America)
107  0x00,
108  0x31, // itu_t_t35_provider_code = 49
109  0x47, 0x41, 0x39,
110  0x34, // ATSC_user_identifier = "GA94"
111  0x03, // ATSC1_data_user_data_type_code=3
112  0 | 0xc0, // (ui16Len/3) | 0xc0 (to be set; index 16) (each CC character
113  //is 3 bytes)
114  0xFF // em_data = 255
115 };
116 
117 static uint8_t g_itu_t_t35_hdr10p_sei_hdr_hevc[NI_HDR10P_SEI_HDR_HEVC_LEN] = {
118  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
119  0x4e,
120  0x01, // nal_unit_header() {forbidden bit=0 nal_unit_type=39,
121  // nuh_layer_id=0 nuh_temporal_id_plus1=1)
122  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
123  0x00, // payLoadSize; to be set (index 7)
124  0xb5, // u8 itu_t_t35_country_code =181 (North America)
125  //0x00,
126  //0x3c, // u16 itu_t_t35_provider_code = 0x003c
127  //0x00,
128  //0x01, // u16 itu_t_t35_provider_oriented_code = 0x0001
129  // payLoadSize count starts from itu_t_t35_provider_code and goes until
130  // and including trailer
131 };
132 
133 static uint8_t g_itu_t_t35_cc_sei_hdr_h264[NI_CC_SEI_HDR_H264_LEN] = {
134  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
135  0x06, // nal_unit_header() {forbidden bit=0 nal_ref_idc=0, nal_unit_type=6
136  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
137  0 + 11, // payLoadSize= ui16Len + 11; to be set (index 6)
138  0xb5, // itu_t_t35_country_code =181 (North America)
139  0x00,
140  0x31, // itu_t_t35_provider_code = 49
141  0x47, 0x41, 0x39,
142  0x34, // ATSC_user_identifier = "GA94"
143  0x03, // ATSC1_data_user_data_type_code=3
144  0 | 0xc0, // (ui16Len/3) | 0xc0 (to be set; index 15) (each CC character
145  //is 3 bytes)
146  0xFF // em_data = 255
147 };
148 
149 static uint8_t g_itu_t_t35_hdr10p_sei_hdr_h264[NI_HDR10P_SEI_HDR_H264_LEN] = {
150  0x00, 0x00, 0x00, 0x01, // NAL start code 00 00 00 01
151  0x06, // nal_unit_header() {forbidden bit=0 nal_ref_idc=0, nal_unit_type=6
152  0x04, // payloadType= 4 (user_data_registered_itu_t_t35)
153  0x00, // payLoadSize; to be set (index 6)
154  0xb5, // itu_t_t35_country_code =181 (North America)
155  //0x00,
156  //0x3c, // u16 itu_t_t35_provider_code = 0x003c
157  //0x00,
158  //0x01, // u16 itu_t_t35_provider_oriented_code = 0x0001
159  // payLoadSize count starts from itu_t_t35_provider_code and goes until
160  // and including trailer
161 };
162 
163 static uint8_t g_sei_trailer[NI_CC_SEI_TRAILER_LEN] = {
164  0xFF, // marker_bits = 255
165  0x80 // RBSP trailing bits - rbsp_stop_one_bit and 7 rbsp_alignment_zero_bit
166 };
167 
168 #define NI_XCODER_FAILURES_MAX 25
169 #define DP_IPC_PASSTHRU 0xFFFFFFFF
170 
171 #ifdef _WIN32
172 #ifdef XCODER_SELF_KILL_ERR
173 #undef XCODER_SELF_KILL_ERR
174 #endif
175 #endif
176 
177 void SwapSWBytes(uint8_t *buf, uint32_t bytes)
178 {
179  uint32_t i;
180  for (i = 0; i < bytes; i += 2)
181  {
182  uint8_t val = buf[i];
183  buf[i] = buf[i + 1];
184  buf[i + 1] = val;
185  }
186 }
187 
188 void SwapSW16(uint16_t *buf, uint32_t bytes)
189 {
190  uint32_t i, words = (bytes + 1) / 2;
191 
192  for (i = 0; i < words; i += 2)
193  {
194  uint16_t val = buf[i];
195  buf[i] = buf[i + 1];
196  buf[i + 1] = val;
197  }
198 }
199 
200 void SwapSW32(uint32_t *buf, uint32_t bytes)
201 {
202  uint32_t i, words = (bytes + 3) / 4;
203  for (i = 0; i < words; i += 2)
204  {
205  uint32_t val = buf[i];
206  buf[i] = buf[i + 1];
207  buf[i + 1] = val;
208  }
209 }
210 
211 void SwapSW64(uint64_t *buf, uint64_t bytes)
212 {
213  uint64_t i, words = (bytes + 7) / 8;
214  for (i = 0; i < words; i += 2)
215  {
216  uint64_t val = buf[i];
217  buf[i] = buf[i + 1];
218  buf[i + 1] = val;
219  }
220 }
221 
222 static void memcpyToPelByBits (uint8_t *dst_u8, uint8_t *src, int width, int height, int dst_stride, int src_stride, int srcValidBitsDepth)
223 {
224  int i, j;
225  uint8_t bitsDepth = srcValidBitsDepth;
226  uint32_t mask = (1<<bitsDepth)-1;
227  uint16_t *dst = (uint16_t *)dst_u8;
228 
229  //fast for byte write
230  if (bitsDepth == 8)
231  {
232  for (j = 0; j < height; j ++)
233  {
234  for (i = 0; i< width; i ++)
235  dst_u8[i] = src[i] & 0xff;
236 
237  src += src_stride;
238  dst_u8 += dst_stride;
239  }
240  return;
241  }
242 
243  //for arbitrary bits
244  for (j = 0; j < height; j ++)
245  {
246  uint8_t *src_line = src;
247  uint32_t cache = 0, cache_bits = 0;
248  for (i = 0; i< width; i ++)
249  {
250  while(cache_bits < bitsDepth)
251  {
252  cache |= ((*src_line++) << cache_bits);
253  cache_bits += 8;
254  }
255 
256  if (cache_bits >= bitsDepth)
257  {
258  dst[i] = cache & mask;
259  cache >>= bitsDepth;
260  cache_bits -= bitsDepth;
261  }
262  }
263 
264  src += src_stride;
265  dst += dst_stride;
266  }
267 }
268 
269 /*
270 static void write_(FILE *fp, uint8_t *data, int32_t width, int32_t height, int32_t stripe)
271 {
272  int32_t i;
273 
274  for (i = 0; i < height; i++)
275  {
276  if (fwrite(data, sizeof(uint8_t ), width, fp) < (size_t)width)
277  {
278  return;
279  }
280  data += stripe;
281  }
282 }
283 */
284 
285 static void trace_recon_tile2raster_y(FILE *fRecon, uint8_t *mem, int width, int height,
286  int leftOffset, int topOffset,
287  int src_stride, int pixDepth, uint8_t *luma_mem_temp)
288 {
289  int i;
290  int write_data_len = 0;
291  int bytesPerPix = (pixDepth == 8 ? 1 : 2);
292  uint8_t *tmp_mem = (uint8_t *)malloc(bytesPerPix*src_stride);
293  int num_tiles = (leftOffset + width + 3) / 4 - leftOffset/4;
294  if(tmp_mem == NULL)
295  return;
296  mem += topOffset/4*4*src_stride + leftOffset/4*16*pixDepth/8;
297  for(i = topOffset/4*4; i < topOffset + height; i += 4) {
298  memcpyToPelByBits(tmp_mem, mem, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
299  if(i >= topOffset && i < topOffset + height){
300  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
301  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
302  write_data_len += width * bytesPerPix;
303  }
304  memcpyToPelByBits(tmp_mem, mem+4*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
305  if(i+1 >= topOffset && i+1 < topOffset + height)
306  {
307  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
308  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
309  write_data_len += width * bytesPerPix;
310  }
311  memcpyToPelByBits(tmp_mem, mem+8*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
312  if(i+2 >= topOffset && i+2 < topOffset + height)
313  {
314  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
315  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
316  write_data_len += width * bytesPerPix;
317  }
318  memcpyToPelByBits(tmp_mem, mem+12*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
319  if(i+3 >= topOffset && i+3 < topOffset + height)
320  {
321  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
322  memcpy(luma_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
323  write_data_len += width * bytesPerPix;
324  }
325  mem += 4*src_stride;
326  }
327  free(tmp_mem);
328 }
329 
330 static void trace_recon_tile2raster_uv(FILE *fRecon, uint8_t *mem_uv, int width, int height,
331  int leftOffset, int topOffset,
332  int src_stride, int pixDepth, uint8_t *ch_mem_temp)
333 {
334  int i, j, k;
335  int bytesPerPix = (pixDepth == 8 ? 1 : 2);
336  uint8_t *tmp_mem = (uint8_t *)malloc(bytesPerPix*src_stride*2);
337  int num_tiles = 2*((leftOffset + width + 3) / 4 - leftOffset/4);
338  if(tmp_mem == NULL)
339  return;
340  uint8_t *mem;
341  int uv;
342  int write_data_len = 0;
343  memset(tmp_mem, 0x00, bytesPerPix * src_stride * 2);
344  for(uv = 0; uv <= 1; uv ++) {
345  mem = mem_uv + topOffset/4*8*src_stride + leftOffset/4*32*pixDepth/8;
346  for(i = topOffset/4*4; i < topOffset + height; i += 4) {
347  if(i >= topOffset && i < topOffset + height) {
348  memcpyToPelByBits(tmp_mem, mem, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
349  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
350  if(bytesPerPix == 1)
351  tmp_mem[k] = tmp_mem[j];
352  else
353  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
354  }
355  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
356  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
357  write_data_len += width * bytesPerPix;
358  }
359  if(i+1 >= topOffset && i+1 < topOffset + height) {
360  memcpyToPelByBits(tmp_mem, mem+4*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
361  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
362  if(bytesPerPix == 1)
363  tmp_mem[k] = tmp_mem[j];
364  else
365  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
366  }
367  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
368  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
369  write_data_len += width * bytesPerPix;
370  }
371  if(i+2 >= topOffset && i+2 < topOffset + height) {
372  memcpyToPelByBits(tmp_mem, mem+8*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
373  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
374  if(bytesPerPix == 1)
375  tmp_mem[k] = tmp_mem[j];
376  else
377  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
378  }
379  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
380  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
381  write_data_len += width * bytesPerPix;
382  }
383  memcpyToPelByBits(tmp_mem, mem+12*pixDepth/8, 4, num_tiles, 4, 16*pixDepth/8, pixDepth);
384  if(i+3 >= topOffset && i+3 < topOffset + height) {
385  for(j = uv, k = 0; j < 4 * num_tiles; j += 2, k++) {
386  if(bytesPerPix == 1)
387  tmp_mem[k] = tmp_mem[j];
388  else
389  ((uint16_t*)tmp_mem)[k] = ((uint16_t*)tmp_mem)[j];
390  }
391  // write_(fRecon, tmp_mem + (leftOffset & 3)*bytesPerPix, width*bytesPerPix, 1, src_stride);
392  memcpy(ch_mem_temp + write_data_len, tmp_mem + (leftOffset & 3) * bytesPerPix, width * bytesPerPix);
393  write_data_len += width * bytesPerPix;
394  }
395  mem += 8*src_stride;
396  }
397  }
398  free(tmp_mem);
399 }
400 
401 static double calc_noise(uint8_t *p1, uint8_t *p2, int size)
402 {
403  double sum;
404  double diff;
405  int i;
406 
407  sum = 0;
408  for (i = 0; i < size; i++)
409  {
410  diff = p1[i] - p2[i];
411  sum += diff * diff;
412  }
413 
414  return sum;
415 }
416 
417 /*!*****************************************************************************
418  * \brief Download hw frames by HwDesc.
419  *
420  * \param
421  *
422  * \return 0 if successful, < 0 otherwise
423  ******************************************************************************/
424 static int hwdl_frame(ni_session_context_t *p_ctx,
425  ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame,
426  int pixel_format)
427 {
428  niFrameSurface1_t *src_surf = (niFrameSurface1_t *)(p_src_frame->p_data[3]);
429  int ret = 0;
430 
431  ret = ni_frame_buffer_alloc_dl(&(p_session_data->data.frame),
432  src_surf->ui16width, src_surf->ui16height,
433  pixel_format);
434 
435  if (ret != NI_RETCODE_SUCCESS)
436  {
438  }
439 
440  p_ctx->is_auto_dl = false;
441  ret = ni_device_session_hwdl(p_ctx, p_session_data, src_surf);
442  if (ret <= 0)
443  {
444  ni_frame_buffer_free(&p_session_data->data.frame);
445  return ret;
446  }
447  return ret;
448 }
449 
451 {
452  // char dump_file_tile[256];
453  // char dump_file_raster[256];
454  //FILE *fout_tile = NULL;
455  FILE *fout_raster = NULL;
456  // snprintf(dump_file_tile, sizeof(dump_file_tile), "output-%04ld-tile.yuv", (long)p_ctx->pkt_num - 1);
457  // snprintf(dump_file_raster, sizeof(dump_file_raster), "output-%04ld-raster.yuv", (long)p_ctx->pkt_num - 1);
458  // fout_tile = fopen(dump_file_tile, "wb");
459  // fout_raster = fopen(dump_file_raster, "wb");
460  int bit_depth = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P10LE || p_ctx->pixel_format == NI_PIX_FMT_P010LE) ? 10 : 8;
461 
463  p_ctx->psnr_y = (float)(p_meta->ui16psnr_y / 1000.0);
464 
465  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode != 3 &&
466  (p_meta->reconLumaSize != 0 || p_meta->reconChromaSize != 0))
467  {
468  uint32_t lum_sz = 0;
469  uint32_t chr_sz = 0;
470  uint8_t *luma_mem_temp = NULL;
471  uint8_t *ch_mem_temp = NULL;
472  int width = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->source_width;
473  int height = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->source_height;
474 
475  uint8_t *luma_mem = (uint8_t *)p_packet->p_data + p_packet->data_len - p_meta->reconChromaSize - p_meta->reconLumaSize;
476  uint8_t *ch_mem = (uint8_t *)p_packet->p_data + p_packet->data_len - p_meta->reconChromaSize;
477 
478  //{
479  if (p_meta->reconLumaSize)
480  {
481  luma_mem_temp = (uint8_t *)malloc(p_meta->reconLumaSize);
482  memset(luma_mem_temp, 0x00, p_meta->reconLumaSize);
483  }
484  if (p_meta->reconChromaSize)
485  {
486  ch_mem_temp = (uint8_t *)malloc(p_meta->reconChromaSize);
487  memset(ch_mem_temp, 0x00, p_meta->reconChromaSize);
488  }
489 
490  lum_sz = (2 * p_meta->frameCropTopOffset + height + 3) / 4 * p_meta->reconLumaWidth * 4;
491  chr_sz = (p_meta->frameCropTopOffset + height / 2 + 1) / 2 * p_meta->reconChromaWidth * 4;
492 
496  if (p_meta->reconLumaSize != 0)
497  {
498  SwapSWBytes(luma_mem, lum_sz);
499  SwapSW16((uint16_t *)luma_mem, lum_sz);
500  SwapSW32((uint32_t *)luma_mem, lum_sz);
501  SwapSW64((uint64_t *)luma_mem, lum_sz);
502  trace_recon_tile2raster_y (fout_raster, luma_mem, width, height,
503  2 * p_meta->frameCropLeftOffset, 2 * p_meta->frameCropTopOffset,
504  p_meta->reconLumaWidth, bit_depth, luma_mem_temp);
505  ni_log2(p_ctx, NI_LOG_DEBUG, "lum_sz %d reconLumaSize %d width %d reconLumaWidth %d\n",
506  lum_sz, p_meta->reconLumaSize, width, p_meta->reconLumaWidth);
507  // fflush(fout_raster);
508  }
509 
510  if (p_meta->reconChromaSize != 0)
511  {
512  SwapSWBytes(ch_mem, chr_sz);
513  SwapSW16((uint16_t *)ch_mem, chr_sz);
514  SwapSW32((uint32_t *)ch_mem, chr_sz);
515  SwapSW64((uint64_t *)ch_mem, chr_sz);
516  trace_recon_tile2raster_uv(fout_raster, ch_mem, width / 2, height / 2,
517  p_meta->frameCropLeftOffset, p_meta->frameCropTopOffset,
518  p_meta->reconChromaWidth, bit_depth, ch_mem_temp);
519  ni_log2(p_ctx, NI_LOG_DEBUG, "chr_sz %d reconChromaSize %d reconChromaWidth %d\n",
520  chr_sz, p_meta->reconChromaSize, p_meta->reconChromaWidth);
521  // fflush(fout_raster);
522  }
523 
524  // fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t) + (p_packet->data_len - p_meta->reconLumaSize - p_meta->reconChromaSize),
525  // p_meta->reconLumaSize + p_meta->reconChromaSize, 1, fout_tile);
526 
530  do
531  {
532  int max;
533  double noise_y = 0.0, noise_u = 0.0, noise_v = 0.0, noise_yuv = 0.0;
534  double power_y = 0.0, power_u = 0.0, power_v = 0.0, power_yuv = 0.0;
535  double psnr_y = 0.0, psnr_u = 0.0, psnr_v = 0.0, psnr_yuv = 0.0;
536 
538  // FILE *f_out_raster = (FILE *)fopen(dump_file_raster, "rb");
539  // fread(luma_mem_temp, width * height , 1, f_out_raster);
540  // fread(ch_mem_temp, width * height / 2, 1, f_out_raster);
541  // fflush(f_out_raster);
542  // fclose(f_out_raster);
543 
544  uint8_t *p_y_src_buf = NULL;
545  uint8_t *p_u_src_buf = NULL;
546  uint8_t *p_v_src_buf = NULL;
547  for (int i = 0; i < 120; i++)
548  {
549  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
550  p_ctx->input_frame_fifo[i].pts == p_meta->frame_tstamp)
551  {
552  p_y_src_buf = p_ctx->input_frame_fifo[i].p_input_buffer;
553  p_u_src_buf = p_y_src_buf + p_ctx->input_frame_fifo[i].video_width * p_ctx->input_frame_fifo[i].video_height;
554  p_v_src_buf = p_u_src_buf + (p_ctx->input_frame_fifo[i].video_width / 2) * (p_ctx->input_frame_fifo[i].video_height / 2);
555 
556  p_ctx->input_frame_fifo[i].usable = 1;
557  ni_log2(p_ctx, NI_LOG_DEBUG, "%s %d i %d pkt_num %ld frame_num %ld frame_tstamp %lld\n",
558  __FUNCTION__, __LINE__, i, p_ctx->pkt_num, p_ctx->frame_num, p_meta->frame_tstamp);
559  break;
560  }
561  }
562  if (p_y_src_buf != NULL)
563  {
564  if (p_meta->reconLumaSize != 0)
565  {
566  noise_y = calc_noise(p_y_src_buf, luma_mem_temp, width * height);
567  }
568  if (p_meta->reconChromaSize != 0)
569  {
570  noise_u = calc_noise(p_u_src_buf, ch_mem_temp, (width / 2) * (height / 2));
571  noise_v = calc_noise(p_v_src_buf, ch_mem_temp + (width / 2) * (height / 2), (width / 2) * (height / 2));
572  }
573  noise_yuv = noise_y + noise_u + noise_v;
574 
575  //This is for the case when encoding some very simple picture
576  //this case the quality is very good. sometimes the noise_y may be zero
577  noise_y = noise_y < 1 ? 1 : noise_y;
578  noise_u = noise_u < 1 ? 1 : noise_u;
579  noise_v = noise_v < 1 ? 1 : noise_v;
580  noise_yuv = noise_yuv < 1 ? 1 : noise_yuv;
581 
582  max = (1 << 8) - 1;
583  power_y = 1.0 * max * max * width * height;
584  power_u = power_y / 4.0;
585  power_v = power_y / 4.0;
586  power_yuv = power_y * 3.0 / 2.0;
587 
588  if (p_meta->reconLumaSize != 0)
589  {
590  psnr_y = 10 * log10(power_y / noise_y);
591  }
592  if (p_meta->reconChromaSize != 0)
593  {
594  psnr_u = 10 * log10(power_u / noise_u);
595  psnr_v = 10 * log10(power_v / noise_v);
596  }
597  psnr_yuv = 10 * log10(power_yuv / noise_yuv);
598  p_ctx->psnr_y = psnr_y;
599  p_ctx->psnr_u = psnr_u;
600  p_ctx->psnr_v = psnr_v;
601  p_ctx->average_psnr = psnr_yuv;
602  ni_log2(p_ctx, NI_LOG_DEBUG, "pkt_num %d psnr_y %.4f psnr_u %.4f psnr_v %.4f psnr_yuv %.4f width %d height %d reconLumaSize 0x%x reconChromaSize 0x%x\n",
603  p_ctx->pkt_num, psnr_y, psnr_u, psnr_v, psnr_yuv, width, height, p_meta->reconLumaSize, p_meta->reconChromaSize);
604  }
605  } while(0);
606 
607  if (luma_mem_temp)
608  {
609  free(luma_mem_temp);
610  luma_mem_temp = NULL;
611  }
612  if (ch_mem_temp)
613  {
614  free(ch_mem_temp);
615  ch_mem_temp = NULL;
616  }
617  // fflush(fout_tile);
618  // fclose(fout_tile);
619  // fflush(fout_raster);
620  // fclose(fout_raster);
621  //}
622  }
623 
624  p_packet->data_len -= p_meta->reconLumaSize + p_meta->reconChromaSize;
625 }
626 
627 // Check for critical failures.
628 // Invalid parameters or resource busy do not account for failures that cause error count to be incremented so they can be retried indefinitely
629 static ni_retcode_t check_err_rc(
630  ni_session_context_t* ctx, ni_retcode_t rc, ni_session_statistic_t *stat_full, int opcode, uint32_t type, int hw_id, uint32_t *inst_id, int opt, const char* func, int line)
631 {
632  ni_retcode_t retval = rc;
633  ni_session_stats_t stat = {0};
634  uint16_t ui16ErrorCount = 0;
635  uint32_t ui32LastErrorStatus = 0;
636 
637  if(opt == OPT_1 || opt == OPT_3)
638  {
639  retval = ni_query_session_stats(ctx, type, &stat, rc, opcode);
640  ui16ErrorCount = stat.ui16ErrorCount;
641  ui32LastErrorStatus = stat.ui32LastErrorStatus;
642  }
643 
644  if (retval != NI_RETCODE_SUCCESS)
645  {
646  ni_log2(ctx, NI_LOG_ERROR, "Query for statistics failed with %d!\n", retval);
647  return NI_RETCODE_FAILURE;
648  }
649 
650  if(opt == OPT_1)
651  {
652  retval = ni_nvme_check_error_code(stat.ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
653 
654  if(!retval)
655  retval = ni_nvme_check_error_code(ui32LastErrorStatus, opcode, type, hw_id, inst_id);
656  }
657  else if(opt == OPT_2){
658  if(!stat_full){
659  ni_log2(ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
661  }
662  retval = ni_nvme_check_error_code(stat_full->ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
663  ui16ErrorCount = stat_full->ui16ErrorCount;
664  ui32LastErrorStatus = stat_full->ui32LastErrorStatus;
665  /* re-query error status for transactionId 0xFFFF */
666  if(stat_full->ui32LastErrorTransactionId == 0xFFFF &&
667  ui16ErrorCount > 0 && !retval &&
668  type == NI_DEVICE_TYPE_DECODER)
669  {
670  retval = ni_nvme_check_error_code(ui32LastErrorStatus, opcode, type, hw_id, inst_id);
671  }
672  }
673  else if(opt == OPT_3)
674  {
675  retval = ni_nvme_check_error_code(stat.ui32LastTransactionCompletionStatus, opcode, type, hw_id, inst_id);
676  }
677 
678  if (retval)
679  {
680  ni_log2(ctx,
681  NI_LOG_ERROR,
682  "Persistent failures detected, %s() line-%d: session_no 0x%x "
683  "sess_err_count %u inst_err_no %u\n",
684  func, line, *(inst_id), ui16ErrorCount,
685  ui32LastErrorStatus);
686 #ifdef XCODER_SELF_KILL_ERR
687  if(opt != OPT_3)
688  {
689  kill(getpid(), SIGTERM);
690  }
691 #endif
692  }
693 
694  return retval;
695 }
696 
697 //Following macros will only check for critical failures.
698 //After the macro runs, rc should contain the status of the last command sent
699 //For non-critical failures, it is assumed in the code that eventually after enough retries, a command will succeed.
700 //Invalid parameters or resource busy do not account for failures that cause error count to be incremented so they can be retried indefinitely
701 #define CHECK_ERR_RC(ctx, rc, info, opcode, type, hw_id, inst_id, opt) \
702  { \
703  (rc) = check_err_rc(ctx, rc, info, opcode, type, hw_id, inst_id, opt, __func__, __LINE__); \
704  if((rc) && ((opt) != OPT_3)) LRETURN; \
705  }
706 
707 #define CHECK_VPU_RECOVERY(ret) \
708  { \
709  if (NI_RETCODE_NVME_SC_VPU_RECOVERY == (ret)) \
710  { \
711  ni_log(NI_LOG_ERROR, "Error, vpu reset.\n"); \
712  (ret) = NI_RETCODE_ERROR_VPU_RECOVERY; \
713  LRETURN; \
714  } \
715  }
716 
717 // ctx->decoder_low_delay is used as condition wait timeout for both decoder
718 // and encoder send/recv multi-thread in low delay mode.
719 static void low_delay_wait(ni_session_context_t* p_ctx)
720 {
721  const char *name = p_ctx->device_type == NI_DEVICE_TYPE_DECODER ? \
722  "decoder" : "encoder";
723  if (p_ctx->async_mode && p_ctx->decoder_low_delay > 0)
724  {
725  int ret;
726  uint64_t abs_time_ns;
727  struct timespec ts;
728 
729  ni_log2(p_ctx, NI_LOG_DEBUG, "%s waiting for %s recv thread\n", __FUNCTION__, name);
730 
731  abs_time_ns = ni_gettime_ns();
732  abs_time_ns += p_ctx->decoder_low_delay * 1000000LL;
733  ts.tv_sec = abs_time_ns / 1000000000LL;
734  ts.tv_nsec = abs_time_ns % 1000000000LL;
735 
737  while (p_ctx->low_delay_sync_flag)
738  {
739  // In case of dead lock on waiting for notification from recv thread.
742  &p_ctx->low_delay_sync_mutex, &ts);
743  ni_pthread_mutex_lock(&p_ctx->mutex);
744  if (ret == ETIMEDOUT)
745  {
746  p_ctx->low_delay_sync_flag = 0;
747  }
748  }
750  }
751 }
752 
753 static void low_delay_signal(ni_session_context_t* p_ctx)
754 {
755  const char *name = p_ctx->device_type == NI_DEVICE_TYPE_DECODER ? \
756  "decoder" : "encoder";
757  if (p_ctx->async_mode && p_ctx->decoder_low_delay > 0)
758  {
759  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: wake up %s send thread\n", __FUNCTION__, name);
761  p_ctx->low_delay_sync_flag = 0;
764  }
765 }
766 
767 static void query_sleep(ni_session_context_t* p_ctx)
768 {
769  if (p_ctx->async_mode)
770  {
771  // To avoid IO spam on NP core from queries and high volumens on latency.
774  ni_pthread_mutex_lock(&p_ctx->mutex);
775  }
776 }
777 
778 // create folder bearing the card name (nvmeX) if not existing
779 // start working inside this folder: nvmeX
780 // find the earliest saved and/or non-existing stream folder and use it as
781 // the pkt saving destination; at most 128 such folders to be checked/created;
782 // folder name is in the format of: streamY, where Y is [1, 128]
783 static void decoder_dump_dir_open(ni_session_context_t *p_ctx)
784 {
785 #ifdef _WIN32
786 #elif __linux__ || __APPLE__
787  FILE *fp;
788  char dir_name[128] = {0};
789  char file_name[512] = {0};
790  ni_device_context_t *p_device_context;
791  DIR *dir;
792  struct dirent *stream_folder;
793  int curr_stream_idx = 0;
794  int earliest_stream_idx = 0;
795  int max_exist_idx = 0;
796  time_t earliest_time = 0;
797  struct stat file_stat;
798 
799  p_device_context =
801  if (!p_device_context)
802  {
803  ni_log2(p_ctx, NI_LOG_ERROR, "Error retrieve device context for decoder guid %d\n",
804  p_ctx->hw_id);
805  return;
806  }
807 
808  flock(p_device_context->lock, LOCK_EX);
809 
810  strcpy(dir_name, &p_ctx->dev_xcoder_name[5]);
811  if (0 != access(dir_name, F_OK))
812  {
813  if (0 != mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO))
814  {
815  ni_log2(p_ctx, NI_LOG_ERROR, "Error create folder %s, errno %d\n", dir_name,
816  NI_ERRNO);
817  } else
818  {
819  ni_log2(p_ctx, NI_LOG_DEBUG, "Created pkt folder for: %s\n", dir_name);
820  }
821  }
822 
823  if (NULL == (dir = opendir(dir_name)))
824  {
825  ni_log2(p_ctx, NI_LOG_ERROR, "Error %d: failed to open directory %s\n",
826  NI_ERRNO, dir_name);
827  } else
828  {
829  // have a quick first pass of streamY folders, and if existing Y < 128,
830  // create a new folder Y+1 directly without checking existing ones'
831  // content
832  while ((stream_folder = readdir(dir)))
833  {
834  if (!strncmp(stream_folder->d_name, "stream", strlen("stream")))
835  {
836  curr_stream_idx =
837  atoi(&(stream_folder->d_name[strlen("stream")]));
838  if (curr_stream_idx > 0)
839  {
840  if (curr_stream_idx > max_exist_idx)
841  {
842  max_exist_idx = curr_stream_idx;
843  }
844  if (NI_MAX_CONTEXTS_PER_HW_INSTANCE == curr_stream_idx)
845  {
846  break;
847  }
848  }
849  }
850  }
851 
852  // if less than 128 streams created then create a new one, otherwise have
853  // to pick the stream folder that has the earliest modified file which
854  // is most likely done by finished session.
855  if (max_exist_idx < NI_MAX_CONTEXTS_PER_HW_INSTANCE)
856  {
857  curr_stream_idx = max_exist_idx + 1;
858  } else
859  {
860  rewinddir(dir);
861  while ((stream_folder = readdir(dir)))
862  {
863  // go through each of these streamY folders and get modified
864  // time of the first pkt-* file to simplify the searching
865  if (!strncmp(stream_folder->d_name, "stream", strlen("stream")))
866  {
867  snprintf(file_name, sizeof(file_name), "%s/%s/pkt-0001.bin",
868  dir_name, stream_folder->d_name);
869 
870  curr_stream_idx =
871  atoi(&(stream_folder->d_name[strlen("stream")]));
872 
873  if (curr_stream_idx > 0 && 0 == access(file_name, F_OK))
874  {
875  // just take pkt-0001 file timestamp to simplify search
876  if (stat(file_name, &file_stat))
877  {
878  ni_log2(p_ctx, NI_LOG_ERROR, "Error %d: failed to stat file %s\n",
879  NI_ERRNO,
880  file_name);
881  } else
882  {
883  if (0 == earliest_stream_idx ||
884  file_stat.st_mtime < earliest_time)
885  {
886  earliest_stream_idx = curr_stream_idx;
887  earliest_time = file_stat.st_mtime;
888  }
889  }
890  } // check first file in streamX
891  } // go through each streamX folder
892  } // read all files in nvmeY
893 
894  curr_stream_idx = earliest_stream_idx;
895 
896  // set the access/modified time of chosen pkt file to NOW so its
897  // stream folder won't be taken by other sessions.
898  snprintf(file_name, sizeof(file_name), "%s/stream%03d/pkt-0001.bin",
899  dir_name, curr_stream_idx);
900  if (utime(file_name, NULL))
901  {
902  ni_log2(p_ctx, NI_LOG_ERROR, "Error utime %s\n", file_name);
903  }
904  } // 128 streams in nvmeY already
905  closedir(dir);
906  }
907 
908  snprintf(p_ctx->stream_dir_name, sizeof(p_ctx->stream_dir_name),
909  "%s/stream%03d", dir_name, curr_stream_idx);
910 
911  if (0 != access(p_ctx->stream_dir_name, F_OK))
912  {
913  if (0 != mkdir(p_ctx->stream_dir_name, S_IRWXU | S_IRWXG | S_IRWXO))
914  {
915  ni_log2(p_ctx, NI_LOG_ERROR, "Error create stream folder %s, errno %d\n",
916  p_ctx->stream_dir_name, NI_ERRNO);
917  } else
918  {
919  ni_log2(p_ctx, NI_LOG_DEBUG, "Created stream sub folder: %s\n",
920  p_ctx->stream_dir_name);
921  }
922  } else
923  {
924  ni_log2(p_ctx, NI_LOG_DEBUG, "Reusing stream sub folder: %s\n",
925  p_ctx->stream_dir_name);
926  }
927 
928  flock(p_device_context->lock, LOCK_UN);
929  ni_rsrc_free_device_context(p_device_context);
930 
931  snprintf(file_name, sizeof(file_name), "%s/process_session_id.txt",
932  p_ctx->stream_dir_name);
933 
934  fp = fopen(file_name, "wb");
935  if (fp)
936  {
937  char number[64] = {'\0'};
938  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pkt dump log created: %s\n", file_name);
939  snprintf(number, sizeof(number), "proc id: %ld\nsession id: %u\n",
940  (long)getpid(), p_ctx->session_id);
941  fwrite(number, strlen(number), 1, fp);
942  fclose(fp);
943  } else
944  {
945  ni_log2(p_ctx, NI_LOG_ERROR, "Error create decoder pkt dump log: %s\n", file_name);
946  }
947 #endif
948 }
949 
950 
951 // open netint p2p driver and fill the pcie address to p_ctx
952 static ni_retcode_t p2p_fill_pcie_address(ni_session_context_t *p_ctx)
953 {
954 #ifdef _WIN32
955  (void)p_ctx;
956  return NI_RETCODE_FAILURE;
957 #else
958  int ret = 0;
959  char line[256];
960  char syspath[256];
961  struct stat bstat;
962  char *p_dev;
963  char *dom, *bus, *dev, *fnc;
964  FILE *fp;
965 
966  if(!p_ctx)
967  {
968  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() parameter is NULL\n",__func__);
969  return NI_RETCODE_FAILURE;
970  }
971 
972  p_ctx->netint_fd = open("/dev/netint", O_RDWR);
973  if (p_ctx->netint_fd < 0)
974  {
975  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Can't open device /dev/netint\n");
976  return NI_RETCODE_FAILURE;
977  }
978 
979  p_dev = &p_ctx->dev_xcoder_name[0];
980  if (stat(p_dev, &bstat) < 0)
981  {
982  ni_log2(p_ctx, NI_LOG_ERROR, "failed to get stat of file %s\n", p_dev);
983  return NI_RETCODE_FAILURE;
984  }
985 
986  if ((bstat.st_mode & S_IFMT) != S_IFBLK)
987  {
988  ni_log2(p_ctx, NI_LOG_ERROR, "%s is not a block device\n", p_dev);
989  return NI_RETCODE_FAILURE;
990  }
991 
992 #ifdef _ANDROID
993  ret = snprintf(syspath, sizeof(syspath) - 1,
994  "/sys/block/%s/device/address",
995  p_dev + 5);
996  syspath[ret] = '\0';
997 
998  fp = fopen(syspath, "r");
999 #else
1000  ret = snprintf(syspath, sizeof(syspath) - 1,
1001  "udevadm info -q path -n %s | perl -nle'print $& "
1002  "while m{(?<=/)[0-9a-f]{4}:[0-9a-f]{2}:[0-9a-f]{2}\\.[0-9a-f]}g' | tail -n 1",
1003  p_dev + 5);
1004  syspath[ret] = '\0';
1005 
1006  fp = popen(syspath, "r");
1007 #endif
1008 
1009  if (fp == NULL)
1010  {
1011  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read address\n");
1012  return NI_RETCODE_FAILURE;
1013  }
1014 
1015  if (fgets(line, 256, fp) == NULL)
1016  {
1017  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read line from address\n");
1018 #ifdef _ANDROID
1019  fclose(fp);
1020 #else
1021  pclose(fp);
1022 #endif
1023  return NI_RETCODE_FAILURE;
1024  }
1025 
1026 #ifdef _ANDROID
1027  fclose(fp);
1028 #else
1029  pclose(fp);
1030 #endif
1031 
1032  errno = 0;
1033  p_ctx->domain = strtoul(line, &dom, 16);
1034  if (errno < 0)
1035  {
1036  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI domain\n");
1037  return NI_RETCODE_FAILURE;
1038  }
1039 
1040  errno = 0;
1041  p_ctx->bus = strtoul(dom + 1, &bus, 16);
1042  if (errno < 0)
1043  {
1044  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI bus\n");
1045  return NI_RETCODE_FAILURE;
1046  }
1047 
1048  errno = 0;
1049  p_ctx->dev = strtoul(bus + 1, &dev, 16);
1050 
1051  if (errno < 0)
1052  {
1053  ni_log2(p_ctx, NI_LOG_ERROR, "Failed to read PCI device\n");
1054  return NI_RETCODE_FAILURE;
1055  }
1056 
1057  errno = 0;
1058  p_ctx->fn = strtoul(dev + 1, &fnc, 16);
1059 
1060  if (errno < 0)
1061  {
1062  ni_log2(p_ctx, NI_LOG_ERROR, "Falied to read PCI function\n");
1063  return NI_RETCODE_FAILURE;
1064  }
1065 
1066  ni_log2(p_ctx, NI_LOG_DEBUG, "PCI slot = %d:%d:%d:%d\n", p_ctx->domain,
1067  p_ctx->bus, p_ctx->dev, p_ctx->fn);
1068 
1069  return NI_RETCODE_SUCCESS;
1070 
1071 #endif
1072 }
1073 
1074 #if __linux__ || __APPLE__
1075 #ifndef _ANDROID
1076 #ifndef DISABLE_BACKTRACE_PRINT
1077 void ni_print_backtrace() {
1078  void* callstack[128];
1079  int frames = backtrace(callstack, 128);
1080  char** strs = backtrace_symbols(callstack, frames);
1081 
1082  ni_log(NI_LOG_ERROR, "Call stack:\n");
1083  for (int i = 0; i < frames; ++i) {
1084  ni_log(NI_LOG_ERROR, "%s\n", strs[i]);
1085  }
1086 
1087  free(strs);
1088 }
1089 #endif
1090 #endif
1091 #endif
1092 
1093 /*!******************************************************************************
1094  * \brief Open a xcoder decoder instance
1095  *
1096  * \param
1097  *
1098  * \return
1099 *******************************************************************************/
1101 {
1103  ni_xcoder_params_t *p_param = NULL;
1104  void* p_buffer = NULL;
1105  uint32_t ui32LBA = 0;
1106  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
1107 
1108  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1109 
1110  if (!p_ctx)
1111  {
1112  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
1113  __func__);
1114  retval = NI_RETCODE_INVALID_PARAM;
1115  LRETURN;
1116  }
1117 
1118  //Create the session if the create session flag is set
1119  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1120  {
1122  p_ctx->pts_table = NULL;
1123  p_ctx->dts_queue = NULL;
1124  p_ctx->p_leftover = NULL;
1125  p_ctx->buffer_pool = NULL;
1126  p_ctx->dec_fme_buf_pool = NULL;
1127  p_ctx->prev_size = 0;
1128  p_ctx->sent_size = 0;
1129  p_ctx->status = 0;
1130  p_ctx->key_frame_type = 0;
1131  p_ctx->ready_to_close = 0;
1132  p_ctx->max_retry_fail_count[0] = p_ctx->max_retry_fail_count[1] = 0;
1133  p_ctx->rc_error_count = 0;
1134  p_ctx->frame_num = 0;
1135  p_ctx->pkt_num = 0;
1136  p_ctx->pkt_index = 0;
1137  p_ctx->session_timestamp = 0;
1138  p_ctx->is_dec_pkt_512_aligned = 0;
1139  p_ctx->p_all_zero_buf = NULL;
1140  p_ctx->last_pkt_pos = 0;
1141  p_ctx->last_frame_offset = 0;
1142  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
1143 
1144  //malloc zero data buffer
1145  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
1147  {
1148  ni_log2(p_ctx, NI_LOG_ERROR,
1149  "ERROR %d: %s() alloc decoder all zero buffer failed\n",
1150  NI_ERRNO, __func__);
1151  retval = NI_RETCODE_ERROR_MEM_ALOC;
1152  LRETURN;
1153  }
1154  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
1155 
1156  //malloc data buffer
1157  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1158  {
1159  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
1160  NI_ERRNO, __func__);
1162  retval = NI_RETCODE_ERROR_MEM_ALOC;
1163  LRETURN;
1164  }
1165  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1166 
1167  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
1168  //In case we can open sesison, the session id would become valid.
1169  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
1170  (uint16_t)NI_INVALID_SESSION_ID;
1171 
1172  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
1173  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_DECODER, ni_htonl(p_ctx->codec_format), p_ctx->hw_action);
1174  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1175  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1176  if (retval != NI_RETCODE_SUCCESS)
1177  {
1178  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
1179  LRETURN;
1180  }
1181  //Open will return a session status structure with a valid session id if it worked.
1182  //Otherwise the invalid session id set before the open command will stay
1183  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
1184  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
1185  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
1186  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
1187  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1188  {
1189  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_ctx->device_handle=0x%" PRIx64 ", "
1190  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1191  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
1193  LRETURN;
1194  }
1195  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder open session ID:0x%x, timestamp:%" PRIu64 "\n",
1196  p_ctx->session_id, p_ctx->session_timestamp);
1197 
1198  //Send keep alive timeout Info
1199  uint64_t keep_alive_timeout =
1200  p_ctx->keep_alive_timeout * 1000000; //send us to FW
1201  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1202  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
1203  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
1204  keep_alive_timeout);
1206  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1207  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1208  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1209  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1210  CHECK_VPU_RECOVERY(retval);
1211 
1212  if (NI_RETCODE_SUCCESS != retval)
1213  {
1214  ni_log2(p_ctx, NI_LOG_ERROR,
1215  "ERROR %s(): nvme write keep_alive_timeout command "
1216  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1217  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1219  LRETURN;
1220  }
1221 
1222  // Send SW version to FW if FW API version is >= 6.2
1224  "62") >= 0)
1225  {
1226  // Send SW version to session manager
1227  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1228  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
1230  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
1231  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
1232  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
1233  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
1234  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1235  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1236  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
1237  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1238  CHECK_VPU_RECOVERY(retval);
1239 
1240  if (NI_RETCODE_SUCCESS != retval)
1241  {
1242  ni_log2(p_ctx, NI_LOG_ERROR,
1243  "ERROR %s(): nvme write sw_version command "
1244  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
1245  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
1247  LRETURN;
1248  }
1249  }
1250 
1251  //VP9 requires a scaler session to be opened internally and attached as
1252  //well
1253  if(p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
1254  {
1255  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding scaling session to Vp9 decoder\n");
1256  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(NI_SCALER_OPCODE_SCALE), ni_htons(p_ctx->session_id));
1257  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1258  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
1259 
1260  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
1261  p_ctx->device_type, p_ctx->hw_id,
1262  &(p_ctx->session_id), OPT_1);
1263  if (NI_RETCODE_SUCCESS != retval)
1264  {
1265  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add vp9 scaler to decoding session\n");
1266  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s():p_ctx->device_handle=0x%" PRIx64 ", "
1267  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n", __func__,
1268  (int64_t)p_ctx->device_handle,p_ctx->hw_id, p_ctx->session_id);
1269  ni_decoder_session_close(p_ctx, 0);
1270  LRETURN;
1271  }
1272  }
1273 
1274  ni_log2(p_ctx, NI_LOG_DEBUG,
1275  "%s(): p_ctx->device_handle=0x%" PRIx64 ", p_ctx->hw_id=%d, "
1276  "p_ctx->session_id=%d\n",
1277  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1278  p_ctx->session_id);
1279  }
1280 
1281  //start dec config
1282  retval = ni_config_instance_set_decoder_params(p_ctx, 0);
1283  if (NI_RETCODE_SUCCESS != retval)
1284  {
1285  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_config_instance_set_decoder_params(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n", (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
1286  //ni_decoder_session_close(p_ctx, 0); //close happens on above
1288  LRETURN;
1289  }
1290  //end dec config
1291 
1292  // init for frame pts calculation
1293  p_ctx->is_first_frame = 1;
1294  p_ctx->last_pts = NI_NOPTS_VALUE;
1295  p_ctx->last_dts = NI_NOPTS_VALUE;
1296  p_ctx->last_dts_interval = 0;
1297  p_ctx->last_pts_interval = 0;
1298  p_ctx->pts_correction_last_dts = INT64_MIN;
1299  p_ctx->pts_correction_last_pts = INT64_MIN;
1300 
1301  //p_ctx->p_leftover = malloc(NI_MAX_PACKET_SZ * 2);
1302  p_ctx->p_leftover = malloc(p_ctx->max_nvme_io_size * 2);
1303  if (!p_ctx->p_leftover)
1304  {
1305  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate leftover buffer.\n",
1306  __func__);
1307  retval = NI_RETCODE_ERROR_MEM_ALOC;
1308  //ni_decoder_session_close(p_ctx, 0);
1309  LRETURN;
1310  }
1311 
1312  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
1313  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
1314 
1315  if (p_ctx->p_session_config)
1316  {
1317  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1318  ni_params_print(p_param);
1319 
1320  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1321  {
1322  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
1323  if (NI_RETCODE_SUCCESS != retval)
1324  {
1325  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1326  __func__, strerror(NI_ERRNO));
1327  LRETURN;
1328  }
1329  }
1330  }
1331 
1332  if (p_ctx->force_low_delay)
1333  {
1334  if (ni_cmp_fw_api_ver(
1336  "6r3") < 0)
1337  {
1338  p_ctx->force_low_delay = false; // forceLowDelay not available for fw < 6r3
1339  ni_log2(p_ctx, NI_LOG_INFO, "Warn %s(): forceLowDelay is not available for fw < 6r3\n",
1340  __func__);
1341  }
1342  }
1343 
1344  p_ctx->active_video_width = 0;
1345  p_ctx->active_video_height = 0;
1346  p_ctx->actual_video_width = 0;
1347  p_ctx->pixel_format_changed = 0;
1348 
1349  ni_log2(p_ctx, NI_LOG_DEBUG,
1350  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1351  "p_ctx->session_id=%d\n",
1352  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1353  p_ctx->session_id);
1354 
1355  if (p_param && p_param->dec_input_params.nb_save_pkt)
1356  {
1357  decoder_dump_dir_open(p_ctx);
1358  }
1359 
1360 #ifdef XCODER_DUMP_DATA
1361  char dir_name[256] = {0};
1362 
1363  snprintf(dir_name, sizeof(dir_name), "%ld-%u-dec-fme", (long)getpid(),
1364  p_ctx->session_id);
1365  DIR *dir = opendir(dir_name);
1366  if (!dir && ENOENT == NI_ERRNO)
1367  {
1368  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
1369  ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder frame dump dir created: %s\n", dir_name);
1370  }
1371  if(dir){
1372  closedir(dir);
1373  }
1374 #endif
1375 
1376 END:
1377 
1378  ni_aligned_free(p_buffer);
1379  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1380 
1381  return retval;
1382 }
1383 
1384 /*!******************************************************************************
1385  * \brief send a keep alive message to firmware
1386  *
1387  * \param
1388  *
1389  * \return
1390  *******************************************************************************/
1391 ni_retcode_t ni_send_session_keep_alive(uint32_t session_id, ni_device_handle_t device_handle, ni_event_handle_t event_handle, void *p_data)
1392 {
1393  ni_retcode_t retval;
1394  uint32_t ui32LBA = 0;
1395 
1396  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
1397  if (NI_INVALID_SESSION_ID == session_id)
1398  {
1399  ni_log(NI_LOG_ERROR, "ERROR: %s(): Invalid session ID!, return\n",
1400  __func__);
1402  LRETURN;
1403  }
1404 
1405  if (NI_INVALID_DEVICE_HANDLE == device_handle)
1406  {
1407  ni_log(NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1408  __func__);
1409  retval = NI_RETCODE_INVALID_PARAM;
1410  LRETURN;
1411  }
1412 
1413  ui32LBA = CONFIG_SESSION_KeepAlive_W(session_id);
1414  if (ni_nvme_send_write_cmd(device_handle, event_handle, p_data,
1415  NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1416  {
1417  ni_log(NI_LOG_ERROR, "ERROR: %s(): device_handle=%" PRIx64 " , "
1418  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1419  retval = NI_RETCODE_FAILURE;
1420  }
1421  else
1422  {
1424  "SUCCESS %s(): device_handle=%" PRIx64 " , "
1425  "session_id=%d\n", __func__, (int64_t)device_handle, session_id);
1426  retval = NI_RETCODE_SUCCESS;
1427  }
1428 
1429 END:
1430 
1431  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
1432 
1433  return retval;
1434 }
1435 
1436 /*!******************************************************************************
1437  * \brief Send end of stream signal to the decoder
1438  *
1439  * \param
1440  *
1441  * \return
1442  *******************************************************************************/
1444 {
1445  ni_retcode_t retval;
1446  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1447  if (!p_ctx)
1448  {
1449  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1450  __func__);
1451  retval = NI_RETCODE_INVALID_PARAM;
1452  LRETURN;
1453  }
1454 
1455  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1456  {
1457  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1458  __func__);
1460  LRETURN;
1461  }
1462 
1464  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1465  p_ctx->device_type, p_ctx->hw_id,
1466  &(p_ctx->session_id), OPT_1);
1467  CHECK_VPU_RECOVERY(retval);
1468 
1469 END:
1470 
1471  if (NI_RETCODE_SUCCESS != retval)
1472  {
1473  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1474  }
1475 
1476  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1477 
1478  return retval;
1479 }
1480 
1481 /*!******************************************************************************
1482  * \brief Flush decoder output
1483  *
1484  * \param
1485  *
1486  * \return
1487  *******************************************************************************/
1489 {
1490  ni_retcode_t retval;
1491  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1492 
1493  if (!p_ctx)
1494  {
1495  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters are null!, return\n",
1496  __func__);
1497  retval = NI_RETCODE_INVALID_PARAM;
1498  LRETURN;
1499  }
1500 
1501  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1502  {
1503  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): xcoder instance id < 0, return\n",
1504  __func__);
1506  LRETURN;
1507  }
1508 
1510  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1511  p_ctx->device_type, p_ctx->hw_id,
1512  &(p_ctx->session_id), OPT_1);
1513 
1514 END:
1515 
1516  if (NI_RETCODE_SUCCESS != retval)
1517  {
1518  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
1519  }
1520 
1521  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1522 
1523  return retval;
1524 }
1525 
1526 /*!******************************************************************************
1527  * \brief Close a xcoder decoder instance
1528  *
1529  * \param
1530  *
1531  * \return
1532  *******************************************************************************/
1534 {
1536  void* p_buffer = NULL;
1537  uint32_t ui32LBA = 0;
1538  int i;
1539  ni_xcoder_params_t *p_param = NULL;
1540 
1541  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1542 
1543  if (!p_ctx)
1544  {
1545  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1546  __func__);
1547  return NI_RETCODE_INVALID_PARAM;
1548  }
1549 
1550  ni_pthread_mutex_lock(&p_ctx->mutex);
1551 
1552  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
1553  {
1554  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
1555  retval = NI_RETCODE_SUCCESS;
1556  LRETURN;
1557  }
1558 
1559  if (NI_CODEC_HW_ENABLE == p_ctx->hw_action)
1560  {
1561  ni_session_statistic_t sessionStatistic = {0};
1562  ni_query_session_statistic_info(p_ctx, NI_DEVICE_TYPE_DECODER, &sessionStatistic);
1563  }
1564 
1565  ni_log2(p_ctx, NI_LOG_INFO,
1566  "Decoder_complete_info:session_id 0x%x, total frames input:%u "
1567  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
1574 
1575  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
1576  {
1577  ni_log2(p_ctx, NI_LOG_ERROR, "%s: Invalid session ID, return.\n", __func__);
1578  retval = NI_RETCODE_SUCCESS;
1579  LRETURN;
1580  }
1581 
1582  //malloc data buffer
1583  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
1584  {
1585  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: malloc decoder close data buffer failed\n", NI_ERRNO);
1586  retval = NI_RETCODE_ERROR_MEM_ALOC;
1587  LRETURN;
1588  }
1589 
1590  if (p_ctx->p_session_config)
1591  {
1592  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1593  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
1594  {
1596  if (NI_RETCODE_SUCCESS != retval)
1597  {
1598  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
1599  __func__, strerror(NI_ERRNO));
1600  LRETURN;
1601  }
1602  }
1603  }
1604 
1605  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
1606 
1608 
1609  int retry = 0;
1610  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
1611  {
1612  ni_log2(p_ctx, NI_LOG_DEBUG,
1613  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
1614  "p_ctx->session_id=%d, close_mode=1\n",
1615  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
1616  p_ctx->session_id);
1617 
1619  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
1620  {
1621  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
1624  break;
1625  } else
1626  {
1627  //Close should always succeed
1628  retval = NI_RETCODE_SUCCESS;
1630  break;
1631  }
1632  /*
1633  else if (*((ni_retcode_t *)p_buffer) == RETCODE_SUCCESS)
1634  {
1635  retval = NI_RETCODE_SUCCESS;
1636  p_ctx->session_id = NI_INVALID_SESSION_ID;
1637  break;
1638  }
1639  else
1640  {
1641  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
1642  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
1643  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
1644  }
1645  */
1646  retry++;
1647  }
1648 
1649 END:
1650 
1651  ni_aligned_free(p_buffer);
1653  ni_memfree(p_ctx->p_leftover);
1654 
1655  if (p_ctx->pts_table)
1656  {
1657  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
1658  ni_memfree(p_ctx->pts_table);
1659  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1660  }
1661 
1662  if (p_ctx->dts_queue)
1663  {
1664  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
1665  ni_memfree(p_ctx->dts_queue);
1666  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
1667  }
1668 
1671  p_ctx->buffer_pool = NULL;
1672  p_ctx->dec_fme_buf_pool = NULL;
1673 
1674  for (i = 0; i < NI_FIFO_SZ; i++)
1675  {
1676  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
1677  }
1678 
1679  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
1680  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
1681  p_ctx->session_id);
1682 
1683  low_delay_signal(p_ctx);
1684  ni_pthread_mutex_unlock(&p_ctx->mutex);
1685 
1686  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
1687 
1688  return retval;
1689 }
1690 
1691 /*!******************************************************************************
1692  * \brief Send a video p_packet to decoder
1693  *
1694  * \param
1695  *
1696  * \return
1697  *******************************************************************************/
1699 {
1700  uint32_t sent_size = 0;
1701  uint32_t packet_size = 0;
1702  int current_pkt_size;
1703  int retval = NI_RETCODE_SUCCESS;
1704  ni_xcoder_params_t *p_param;
1705  ni_instance_buf_info_t buf_info = { 0 };
1706  ni_session_statistic_t sessionStatistic = {0};
1707  int query_retry = 0;
1708  uint32_t ui32LBA = 0;
1709 
1710  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
1711 
1712  if ((!p_ctx) || (!p_packet))
1713  {
1714  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
1715  __func__);
1716  return NI_RETCODE_INVALID_PARAM;
1717  }
1718 
1719  ni_pthread_mutex_lock(&p_ctx->mutex);
1720 
1721  if ((NI_INVALID_SESSION_ID == p_ctx->session_id))
1722  {
1723  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
1724  __func__);
1726  LRETURN;
1727  }
1728 
1729  low_delay_wait(p_ctx);
1730 
1731 #ifdef MEASURE_LATENCY
1732  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
1733  {
1734  uint64_t abs_time_ns = ni_gettime_ns();
1736  abs_time_ns, p_packet->dts);
1737  }
1738 #endif
1739 
1740  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
1741  packet_size = p_packet->data_len;
1742  current_pkt_size = packet_size;
1743 
1744 #ifdef XCODER_311
1745  int max_retry = 200; // 20ms
1746  if (p_packet->video_width * p_packet->video_height >= NI_NUM_OF_PIXELS_1440P)
1747  {
1748  max_retry = NI_MAX_TX_RETRIES; // 100ms
1749  }
1750 #endif
1751 
1752  for (;;)
1753  {
1754  query_sleep(p_ctx);
1755 
1756  query_retry++;
1757 
1758 
1760  "65") >= 0)
1761  {
1763  &sessionStatistic);
1764  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
1766  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
1767  CHECK_VPU_RECOVERY(retval);
1768 
1769  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
1770  } else
1771  {
1773  NI_DEVICE_TYPE_DECODER, &buf_info);
1774  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
1775  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1776  CHECK_VPU_RECOVERY(retval);
1777  }
1778 
1779  if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
1780  {
1781  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
1782  retval = NI_RETCODE_FAILURE;
1783  LRETURN;
1784  }
1785 
1786  if (p_ctx->biggest_bitstream_buffer_allocated < buf_info.buf_avail_size)
1787  {
1789  }
1790  if (p_ctx->biggest_bitstream_buffer_allocated < packet_size &&
1792  {
1793  // Reallocate decoder bitstream buffers to accomodate
1795  "66") >= 0)
1796  {
1797  retval = ni_config_instance_set_write_len(p_ctx,
1799  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
1800  (packet_size / NI_MAX_PACKET_SZ + 1) * NI_MAX_PACKET_SZ);
1802  p_ctx->device_type, p_ctx->hw_id,
1803  &(p_ctx->session_id), OPT_3);
1804  CHECK_VPU_RECOVERY(retval);
1805  }
1806  else
1807  {
1808  retval = ni_config_instance_set_decoder_params(p_ctx, packet_size);
1809  }
1810  if (NI_RETCODE_SUCCESS != retval)
1811  {
1812  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): failed to reallocate bitstream\n", __FUNCTION__);
1813  LRETURN;
1814  }
1815  query_retry--;
1816  continue;
1817  }
1818 
1819  if (NI_RETCODE_SUCCESS != retval ||
1820  buf_info.buf_avail_size < packet_size)
1821  {
1822  ni_log2(p_ctx, NI_LOG_TRACE,
1823  "Warning: dec write query fail rc %d or available buf size %u < "
1824  "pkt size %u , retry: %d max_retry_fail_count %d\n",
1825  retval, buf_info.buf_avail_size, packet_size, query_retry, p_ctx->max_retry_fail_count[0]);
1826 #ifdef XCODER_311
1827  if (query_retry > max_retry)
1828 #else
1829  if (query_retry > NI_MAX_TX_RETRIES)
1830 #endif
1831  {
1833  p_ctx->max_retry_fail_count[0]++;
1835  LRETURN;
1836  }
1837  ni_pthread_mutex_unlock(&p_ctx->mutex);
1839  ni_pthread_mutex_lock(&p_ctx->mutex);
1840  }
1841  else
1842  {
1843  p_ctx->max_retry_fail_count[0] = 0;
1844  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec write query success, available buf "
1845  "size %u >= pkt size %u !\n",
1846  buf_info.buf_avail_size, packet_size);
1847  break;
1848  }
1849  }
1850 
1851  //Configure write size for the buffer
1853  packet_size);
1855  p_ctx->device_type, p_ctx->hw_id,
1856  &(p_ctx->session_id), OPT_1);
1857  CHECK_VPU_RECOVERY(retval);
1858  if (retval < 0)
1859  {
1860  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config pkt size command failed\n",
1861  __func__);
1863  LRETURN;
1864  }
1865 
1867 
1868  //check for start of stream flag
1869  if (p_packet->start_of_stream)
1870  {
1872  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1873  p_ctx->device_type, p_ctx->hw_id,
1874  &(p_ctx->session_id), OPT_1);
1875  CHECK_VPU_RECOVERY(retval);
1876  if (NI_RETCODE_SUCCESS != retval)
1877  {
1878  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send SOS.\n", __func__);
1879  LRETURN;
1880  }
1881 
1882  p_packet->start_of_stream = 0;
1883  }
1884 
1885  if (p_packet->p_data)
1886  {
1887  ni_log2(p_ctx, NI_LOG_DEBUG,
1888  "%s() had data to send: packet_size=%u, "
1889  "p_packet->sent_size=%d, p_packet->data_len=%u, "
1890  "p_packet->start_of_stream=%u, p_packet->end_of_stream=%u, "
1891  "p_packet->video_width=%u, p_packet->video_height=%u\n",
1892  __func__, packet_size, p_packet->sent_size, p_packet->data_len,
1893  p_packet->start_of_stream, p_packet->end_of_stream,
1894  p_packet->video_width, p_packet->video_height);
1895 
1896  uint8_t *p_data = (uint8_t *)p_packet->p_data;
1897  // Note: session status is NOT reset but tracked between send
1898  // and recv to catch and recover from a loop condition
1899  // p_ctx->status = 0;
1900 
1901  if (packet_size % NI_MEM_PAGE_ALIGNMENT) //packet size, already aligned
1902  {
1903  packet_size = ( (packet_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
1904  }
1905 
1906  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
1907  p_data, packet_size, ui32LBA);
1908  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write,
1909  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
1910  CHECK_VPU_RECOVERY(retval);
1911  if (retval < 0)
1912  {
1913  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
1915  LRETURN;
1916  }
1917 
1918  // reset session status after successful send
1919  p_ctx->status = 0;
1920 
1921  sent_size = p_packet->data_len;
1922  p_packet->data_len = 0;
1923 
1924  if (p_param->dec_input_params.nb_save_pkt)
1925  {
1926  char dump_file[512] = {0};
1927  long curr_pkt_num =
1928  ((long)p_ctx->pkt_num % p_param->dec_input_params.nb_save_pkt) + 1;
1929  snprintf(dump_file, sizeof(dump_file), "%s/pkt-%04ld.bin",
1930  p_ctx->stream_dir_name, curr_pkt_num);
1931  FILE *f = fopen(dump_file, "wb");
1932  if (f)
1933  {
1934  fwrite(p_packet->p_data, sent_size, 1, f);
1935  fflush(f);
1936  fclose(f);
1937  }
1938  }
1939 
1940  p_ctx->pkt_num++;
1941  p_ctx->low_delay_sync_flag = 1;
1942  }
1943 
1944  //Handle end of stream flag
1945  if (p_packet->end_of_stream)
1946  {
1948  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
1949  p_ctx->device_type, p_ctx->hw_id,
1950  &(p_ctx->session_id), OPT_1);
1951  CHECK_VPU_RECOVERY(retval);
1952 
1953  if (NI_RETCODE_SUCCESS != retval)
1954  {
1955  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Failed to send EOS.\n", __func__);
1956  LRETURN;
1957  }
1958 
1959  p_packet->end_of_stream = 0;
1960  p_ctx->ready_to_close = 1;
1961  }
1962 
1963  if (p_ctx->is_dec_pkt_512_aligned)
1964  {
1965  // save NI_MAX_DEC_REJECT pts values and their corresponding number of 512 aligned data
1966  if (p_ctx->is_first_frame && (p_ctx->pkt_index != -1))
1967  {
1968  p_ctx->pts_offsets[p_ctx->pkt_index] = p_packet->pts;
1969  p_ctx->flags_array[p_ctx->pkt_index] = p_packet->flags;
1970  p_ctx->pkt_offsets_index[p_ctx->pkt_index] = current_pkt_size/512; // assuming packet_size is 512 aligned
1971  p_ctx->pkt_pos[p_ctx->pkt_index] = p_packet->pkt_pos;
1972  ni_log2(p_ctx, NI_LOG_DEBUG,
1973  "%s: pkt_index %d pkt_offsets_index %" PRIu64
1974  " pts_offsets %" PRId64 "\n",
1975  __func__, p_ctx->pkt_index,
1976  p_ctx->pkt_offsets_index[p_ctx->pkt_index],
1977  p_ctx->pts_offsets[p_ctx->pkt_index]);
1978  p_ctx->pkt_index ++;
1979  if (p_ctx->pkt_index >= NI_MAX_DEC_REJECT)
1980  {
1981  ni_log2(p_ctx, NI_LOG_DEBUG,
1982  "%s(): more than NI_MAX_DEC_REJECT frames are rejected by the "
1983  "decoder. Increase NI_MAX_DEC_REJECT is required or default "
1984  "gen pts values will be used !\n",
1985  __func__);
1986  p_ctx->pkt_index = -1; // signaling default pts gen
1987  }
1988  }
1989  }
1990  else
1991  {
1992  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pts;
1993  p_ctx->flags_array[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->flags;
1994  p_ctx->pkt_pos[p_ctx->pkt_index % NI_FIFO_SZ] = p_packet->pkt_pos;
1995  if (p_ctx->pkt_index == 0)
1996  {
1997  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = 0;
1998  /* minus 1 here. ffmpeg parses the msb 0 of long start code as the last packet's payload for hevc bitstream (hevc_parse).
1999  * move 1 byte forward on all the pkt_offset so that frame_offset coming from fw can fall into the correct range. */
2000  //p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size - 1;
2001  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = current_pkt_size;
2002  ni_log2(p_ctx, NI_LOG_DEBUG,
2003  "%s: (first packet) pkt_index %d i %u "
2004  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2005  " pts_offsets %" PRId64 "\n",
2006  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2007  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2008  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2009  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2010  }
2011  else
2012  {
2013  // cumulate sizes to correspond to FW offsets
2014  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index[(p_ctx->pkt_index - 1) % NI_FIFO_SZ];
2015  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index[(p_ctx->pkt_index - 1) % NI_FIFO_SZ] + current_pkt_size;
2016  ni_log2(p_ctx, NI_LOG_DEBUG,
2017  "%s: pkt_index %d i %u pkt_offsets_index_min "
2018  "%" PRIu64 " pkt_offsets_index %" PRIu64 " pts_offsets %" PRId64
2019  "\n",
2020  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2021  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2022  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2023  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2024 
2025  //Wrapping 32 bits since FW send u32 wrapped values
2026  if (p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] > 0xFFFFFFFF)
2027  {
2028  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] - (0x100000000);
2029  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ] = p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ] + current_pkt_size;
2030  ni_log2(p_ctx, NI_LOG_DEBUG,
2031  "%s: (wrap) pkt_index %d i %u "
2032  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index %" PRIu64
2033  " pts_offsets %" PRId64 "\n",
2034  __func__, p_ctx->pkt_index, p_ctx->pkt_index % NI_FIFO_SZ,
2035  p_ctx->pkt_offsets_index_min[p_ctx->pkt_index % NI_FIFO_SZ],
2036  p_ctx->pkt_offsets_index[p_ctx->pkt_index % NI_FIFO_SZ],
2037  p_ctx->pts_offsets[p_ctx->pkt_index % NI_FIFO_SZ]);
2038  }
2039  }
2040 
2041  /* if this wrap-around pkt_offset_index spot is about to be overwritten, free the previous one. */
2042  free(p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ]);
2043 
2044  if (p_packet->p_custom_sei_set)
2045  {
2046  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = malloc(sizeof(ni_custom_sei_set_t));
2047  if (p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ])
2048  {
2049  ni_custom_sei_set_t *p_custom_sei_set = p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ];
2050  memcpy(p_custom_sei_set, p_packet->p_custom_sei_set, sizeof(ni_custom_sei_set_t));
2051  }
2052  else
2053  {
2054  /* warn and lose the sei data. */
2055  ni_log2(p_ctx, NI_LOG_ERROR,
2056  "Error %s: failed to allocate custom SEI buffer for pkt.\n",
2057  __func__);
2058  }
2059  }
2060  else
2061  {
2062  p_ctx->pkt_custom_sei_set[p_ctx->pkt_index % NI_FIFO_SZ] = NULL;
2063  }
2064 
2065  p_ctx->pkt_index++;
2066  }
2067 
2068  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue, p_packet->dts, 0);
2069  if (NI_RETCODE_SUCCESS != retval)
2070  {
2071  ni_log2(p_ctx, NI_LOG_ERROR,
2072  "ERROR %s(): ni_timestamp_register() for dts returned %d\n",
2073  __func__, retval);
2074  }
2075 END:
2076 
2077  ni_pthread_mutex_unlock(&p_ctx->mutex);
2078 
2079  if (NI_RETCODE_SUCCESS == retval)
2080  {
2081  ni_log2(p_ctx, NI_LOG_TRACE,
2082  "%s(): exit: packets: %" PRIu64 " offset %" PRIx64 ""
2083  " sent_size = %u, status=%d\n",
2084  __func__, p_ctx->pkt_num, (uint64_t)p_packet->pos, sent_size,
2085  p_ctx->status);
2086  return sent_size;
2087  } else
2088  {
2089  ni_log2(p_ctx, NI_LOG_ERROR,
2090  "ERROR %s(): exit: returnErr: %d, p_ctx->status: %d\n", __func__,
2091  retval, p_ctx->status);
2092  return retval;
2093  }
2094 }
2095 
2096 static int64_t guess_correct_pts(ni_session_context_t* p_ctx, int64_t reordered_pts, int64_t dts)
2097 {
2098  int64_t pts = NI_NOPTS_VALUE;
2099  if (dts != NI_NOPTS_VALUE)
2100  {
2102  p_ctx->pts_correction_last_dts = dts;
2103  ni_log2(p_ctx, NI_LOG_DEBUG,
2104  "%s: pts_correction_last_dts %" PRId64 " "
2105  "pts_correction_num_faulty_dts %d\n",
2106  __func__, p_ctx->pts_correction_last_dts,
2108  }
2109  else if (reordered_pts != NI_NOPTS_VALUE)
2110  {
2111  p_ctx->pts_correction_last_dts = reordered_pts;
2112  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_dts %" PRId64 "\n", __func__,
2113  p_ctx->pts_correction_last_dts);
2114  }
2115  if (reordered_pts != NI_NOPTS_VALUE)
2116  {
2117  p_ctx->pts_correction_num_faulty_pts += reordered_pts <= p_ctx->pts_correction_last_pts;
2118  p_ctx->pts_correction_last_pts = reordered_pts;
2119  ni_log2(p_ctx, NI_LOG_DEBUG,
2120  "%s: pts_correction_last_pts %" PRId64 " "
2121  "pts_correction_num_faulty_pts %d\n",
2122  __func__, p_ctx->pts_correction_last_pts,
2124  }
2125  else if (dts != NI_NOPTS_VALUE)
2126  {
2127  p_ctx->pts_correction_last_pts = dts;
2128  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: pts_correction_last_pts %" PRId64 "\n", __func__,
2129  p_ctx->pts_correction_last_pts);
2130  }
2132  && reordered_pts != NI_NOPTS_VALUE)
2133  {
2134  pts = reordered_pts;
2135  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (reordered_pts) pts %" PRId64 "\n", __func__,
2136  pts);
2137  }
2138  else
2139  {
2140  if ((NI_NOPTS_VALUE == reordered_pts) ||
2141  (NI_NOPTS_VALUE == p_ctx->last_pts) || (dts >= p_ctx->last_pts))
2142  {
2143  pts = dts;
2144  } else
2145  {
2146  pts = reordered_pts;
2147  }
2148 
2149  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (dts) pts %" PRId64 "\n", __func__, pts);
2150  }
2151  return pts;
2152 }
2153 
2154 static int rotated_array_binary_search(uint64_t *lefts, uint64_t *rights,
2155  int32_t size, uint64_t target)
2156 {
2157  int lo = 0;
2158  int hi = size - 1;
2159  while (lo <= hi)
2160  {
2161  int mid = lo + (hi - lo) / 2;
2162  if (lefts[mid] <= target && target < rights[mid])
2163  {
2164  return mid;
2165  }
2166 
2167  if (rights[mid] == 0)
2168  {
2169  // empty in (mid, hi)
2170  hi = mid - 1;
2171  continue;
2172  }
2173 
2174  if (rights[lo] <= rights[mid])
2175  {
2176  if (lefts[lo] <= target && target < lefts[mid])
2177  {
2178  // Elements are all monotonous in (lo, mid)
2179  hi = mid - 1;
2180  } else
2181  {
2182  // Rotation in (mid, hi)
2183  lo = mid + 1;
2184  }
2185  } else
2186  {
2187  if (rights[mid] <= target && target < rights[hi])
2188  {
2189  // Elements are all monotonous in (mid, hi)
2190  lo = mid + 1;
2191  } else
2192  {
2193  // Rotation in (lo, mid)
2194  hi = mid - 1;
2195  }
2196  }
2197  }
2198 
2199  return -1;
2200 }
2201 
2202 /*!******************************************************************************
2203  * \brief Retrieve a YUV p_frame from decoder
2204  *
2205  * \param
2206  *
2207  * \return
2208  *******************************************************************************/
2210 {
2211  ni_instance_mgr_stream_info_t data = { 0 };
2212  int rx_size = 0;
2213  uint64_t frame_offset = 0;
2214  uint8_t *p_data_buffer = NULL;
2215  int i = 0;
2216  int is_planar;
2217  int retval = NI_RETCODE_SUCCESS;
2218  int metadata_hdr_size = NI_FW_META_DATA_SZ - NI_MAX_NUM_OF_DECODER_OUTPUTS * sizeof(niFrameSurface1_t);
2219  int sei_size = 0;
2220  uint32_t total_bytes_to_read = 0;
2221  uint32_t read_size_bytes = 0;
2222  ni_instance_buf_info_t buf_info = { 0 };
2223  ni_session_statistic_t sessionStatistic = {0};
2224  ni_xcoder_params_t *p_param;
2225 
2226  int query_retry = 0;
2227  uint32_t ui32LBA = 0;
2228  unsigned int bytes_read_so_far = 0;
2229  int query_type = INST_BUF_INFO_RW_READ;
2230  int low_delay_notify = 0;
2231  uint32_t frames_dropped = 0;
2232  uint8_t get_first_metadata = 0;
2233  uint8_t sequence_change = 0;
2234 
2235  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
2236 
2237  if ((!p_ctx) || (!p_frame))
2238  {
2239  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed parameters are null!, return\n");
2240  return NI_RETCODE_INVALID_PARAM;
2241  }
2242 
2243  ni_pthread_mutex_lock(&p_ctx->mutex);
2244 
2245 start:
2246  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
2247  {
2248  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): xcoder instance id < 0, return\n",
2249  __func__);
2251  LRETURN;
2252  }
2253 
2254  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
2255  p_data_buffer = (uint8_t *)p_frame->p_buffer;
2256 
2257  // p_frame->p_data[] can be NULL before actual resolution is returned by
2258  // decoder and buffer pool is allocated, so no checking here.
2259  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2260  p_frame->data_len[2] + metadata_hdr_size;
2261  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to read %u, low_delay %u\n",
2262  total_bytes_to_read, p_ctx->decoder_low_delay);
2263 
2264  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
2265  {
2266  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
2267  p_ctx->frame_num, p_ctx->pkt_num);
2268  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
2269  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
2270  p_ctx->force_low_delay_cnt = frames_dropped;
2271  }
2272  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt >= p_ctx->pkt_num)
2273  {
2274  //nothing to query, leave
2275  retval = NI_RETCODE_SUCCESS;
2276  LRETURN;
2277  }
2278  query_type = INST_BUF_INFO_RW_READ_BUSY;
2279  }
2280  for (;;)
2281  {
2282  query_sleep(p_ctx);
2283 
2284  query_retry++;
2285 
2287  "65") >= 0)
2288  {
2290  &sessionStatistic);
2291  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
2293  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
2294  CHECK_VPU_RECOVERY(retval);
2295 
2296  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
2297  } else
2298  {
2299  retval = ni_query_instance_buf_info(p_ctx, query_type,
2300  NI_DEVICE_TYPE_DECODER, &buf_info);
2301  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2302  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2303  CHECK_VPU_RECOVERY(retval);
2304  }
2305 
2306  ni_log2(p_ctx, NI_LOG_TRACE, "Info query buf_info.size = %u\n",
2307  buf_info.buf_avail_size);
2308 
2309  if (NI_RETCODE_SUCCESS != retval)
2310  {
2311  ni_log2(p_ctx, NI_LOG_TRACE, "Warning: dec read query fail rc %d retry %d max_retry_fail_count %d\n",
2312  retval, query_retry, p_ctx->max_retry_fail_count[1]);
2313 
2314  if (query_retry >= 1000)
2315  {
2316  p_ctx->max_retry_fail_count[1]++;
2317  low_delay_notify = 1;
2319  LRETURN;
2320  }
2321  ni_pthread_mutex_unlock(&p_ctx->mutex);
2323  ni_pthread_mutex_lock(&p_ctx->mutex);
2324  }
2325  else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
2326  {
2327  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
2328  retval = NI_RETCODE_FAILURE;
2329  LRETURN;
2330  }
2331  else if (buf_info.buf_avail_size == metadata_hdr_size)
2332  {
2333  ni_log2(p_ctx, NI_LOG_DEBUG, "Info only metadata hdr is available, seq change?\n");
2334  total_bytes_to_read = metadata_hdr_size;
2335  sequence_change = 1;
2336  break;
2337  }
2338  else if (0 == buf_info.buf_avail_size)
2339  {
2340  // query to see if it is eos now, if we have sent it
2341  if (p_ctx->ready_to_close)
2342  {
2343  ni_log2(p_ctx, NI_LOG_TRACE, "Info dec query, ready_to_close %u, query eos\n",
2344  p_ctx->ready_to_close);
2345  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2346  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2347  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2348  CHECK_VPU_RECOVERY(retval);
2349 
2350  if (data.is_flushed ||
2351  query_retry >=
2353  {
2354  if (query_retry >=
2356  {
2357  ni_log2(p_ctx, NI_LOG_ERROR,
2358  "WARNING: Dec eos reached but also exceeded max dec read query "
2359  "retries. is_flushed=%u try=%d.\n",
2360  data.is_flushed, query_retry);
2361  } else
2362  {
2363  ni_log2(p_ctx, NI_LOG_DEBUG,
2364  "Dec eos reached. is_flushed=%u try=%d.\n",
2365  data.is_flushed, query_retry);
2366  }
2367  p_frame->end_of_stream = 1;
2368  low_delay_notify = 1;
2369  retval = NI_RETCODE_SUCCESS;
2370  LRETURN;
2371  } else
2372  {
2373  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0, query try %d,"
2374  " retrying ..\n", query_retry);
2375  ni_pthread_mutex_unlock(&p_ctx->mutex);
2377  ni_pthread_mutex_lock(&p_ctx->mutex);
2378  continue;
2379  }
2380  }
2381 
2382  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read available buf size == 0. retry=%d, eos=%d"
2383  "\n", query_retry, p_frame->end_of_stream);
2385  (p_ctx->decoder_low_delay > 0 &&
2386  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
2387  < p_ctx->pkt_num))) &&
2388  query_retry < 1000 / 2)
2389  {
2390  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
2391  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
2392  // last pkt sent to decoder marked as dropped, no output,
2393  // so just stop query and return
2394  p_ctx->force_low_delay_cnt++;
2395  low_delay_signal(p_ctx);
2396  retval = NI_RETCODE_SUCCESS;
2397  LRETURN;
2398  }
2399  }
2400  ni_pthread_mutex_unlock(&p_ctx->mutex);
2401  ni_usleep(25);
2402  ni_pthread_mutex_lock(&p_ctx->mutex);
2403  continue;
2404  } else
2405  {
2406  if(p_ctx->decoder_low_delay > 0)
2407  {
2408  if (p_ctx->force_low_delay) {
2409  p_ctx->force_low_delay_cnt++;
2410  low_delay_signal(p_ctx);
2411  } else {
2412  ni_log2(p_ctx, NI_LOG_ERROR,
2413  "Warning: ceased using low delay decoding mode after "
2414  "excessively long decoder read query.\n");
2415  // Here it should be the last signal to release the send thread
2416  // holding the low delay mutex.
2417  low_delay_signal(p_ctx);
2418  p_ctx->decoder_low_delay = 0;
2419  }
2420  }
2421 
2422  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
2423  {
2424  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
2426  {
2427  if(query_retry <= 2000)
2428  {
2429  ni_pthread_mutex_unlock(&p_ctx->mutex);
2430  ni_usleep(25);
2431  ni_pthread_mutex_lock(&p_ctx->mutex);
2432  continue;
2433  } else {
2434  p_ctx->pkt_delay_cnt++;
2435  ni_log2(p_ctx, NI_LOG_ERROR,
2436  "Warning: decoder pkt_num %u frame_num %u "
2437  "timeout, increaing pkt_delay_cnt to %u\n",
2438  p_ctx->pkt_num, p_ctx->frame_num,
2439  p_ctx->pkt_delay_cnt);
2440  }
2441  }
2442  }
2443 
2444  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read failed %d retries. rc=%d; eos=%d\n",
2445  query_retry, p_ctx->status, p_frame->end_of_stream);
2446  }
2447  retval = NI_RETCODE_SUCCESS;
2448  LRETURN;
2449  }
2450  else
2451  {
2452  // We have to ensure there are adequate number of DTS for picture
2453  // reorder delay otherwise wait for more packets to be sent to decoder.
2454  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
2455  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
2456  !p_ctx->ready_to_close &&
2458  {
2459  retval = NI_RETCODE_SUCCESS;
2460  ni_log2(p_ctx, NI_LOG_DEBUG,
2461  "At least %d packets should be sent before reading the "
2462  "first frame!\n",
2463  p_ctx->pic_reorder_delay + 1);
2464  LRETURN;
2465  }
2466  p_ctx->max_retry_fail_count[1] = 0;
2467 
2468  // get actual YUV transfer size if this is the stream's very first read
2469  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
2470  {
2471  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
2472  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
2473  p_ctx->device_type, p_ctx->hw_id,
2474  &(p_ctx->session_id), OPT_1);
2475  CHECK_VPU_RECOVERY(retval);
2476 
2477  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
2478  "%ux%u frame-rate %u is_flushed %u\n",
2479  data.picture_width, data.picture_height,
2481  data.frame_rate, data.is_flushed);
2484  p_ctx->actual_video_width = data.picture_width;
2485  p_ctx->pixel_format = data.pix_format;
2486  is_planar = (p_ctx->pixel_format == NI_PIX_FMT_YUV420P) ||
2489  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
2490  p_ctx->is_first_frame = 1;
2491  p_ctx->pixel_format_changed = 0;
2492 
2493  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
2494  "%ux%u format = %d\n", p_frame->video_width, p_frame->video_height,
2495  p_ctx->active_video_width, p_ctx->active_video_height, p_ctx->pixel_format);
2496 
2498 
2499  // set up decoder YUV frame buffer pool
2502  p_ctx->active_video_height,
2504  p_ctx->bit_depth_factor))
2505  {
2506  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Cannot allocate fme buf pool.\n",
2507  __func__);
2508  retval = NI_RETCODE_ERROR_MEM_ALOC;
2509 
2510  ni_pthread_mutex_unlock(&p_ctx->mutex);
2511  ni_decoder_session_close(p_ctx, 0);
2512 #ifdef XCODER_SELF_KILL_ERR
2513  // if need to terminate at such occasion when continuing is not
2514  // possible, trigger a codec closure
2515  ni_log2(p_ctx, NI_LOG_ERROR, "Terminating due to unable to allocate fme buf "
2516  "pool.\n");
2517  kill(getpid(), SIGTERM);
2518 #endif
2519  ni_pthread_mutex_lock(&p_ctx->mutex);
2520  LRETURN;
2521  }
2522 
2524  p_ctx->dec_fme_buf_pool, p_frame, 1, // get mem buffer
2525  p_ctx->actual_video_width, p_ctx->active_video_height,
2527  p_ctx->bit_depth_factor, is_planar);
2528 
2529  if (NI_RETCODE_SUCCESS != retval)
2530  {
2531  LRETURN;
2532  }
2533  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
2534  p_frame->data_len[2] + metadata_hdr_size;
2535  p_data_buffer = (uint8_t*) p_frame->p_buffer;
2536 
2537  // make sure we don't read more than available
2538  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
2539  "available: %u\n", p_frame->buffer_size,
2540  total_bytes_to_read, buf_info.buf_avail_size);
2541  }
2542  break;
2543  }
2544  }
2545 
2546  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
2547  "%u cb len %u hdr %d\n",
2548  total_bytes_to_read, p_ctx->max_nvme_io_size,
2549  p_frame->data_len[0], p_frame->data_len[1],
2550  p_frame->data_len[2], metadata_hdr_size);
2551 
2552  if (buf_info.buf_avail_size < total_bytes_to_read)
2553  {
2554  ni_pthread_mutex_unlock(&p_ctx->mutex);
2555  ni_log2(p_ctx, NI_LOG_ERROR,
2556  "ERROR %s() avaliable size(%u)"
2557  "less than needed (%u)\n",
2558  __func__, buf_info.buf_avail_size, total_bytes_to_read);
2559  abort();
2560  }
2561 
2562  if (buf_info.buf_avail_size == metadata_hdr_size && !p_ctx->frame_num)
2563  {
2564  if (ni_cmp_fw_api_ver(
2566  "6rE") >= 0)
2567  {
2568  // allocate p_data_buffer to read the first metadata
2569  void *p_metadata_buffer = NULL;
2570  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
2572  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
2573  {
2574  ni_log2(p_ctx, NI_LOG_ERROR,
2575  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
2576  NI_ERRNO, __func__);
2577  retval = NI_RETCODE_ERROR_MEM_ALOC;
2578  LRETURN;
2579  }
2580  p_data_buffer = (uint8_t *)p_metadata_buffer;
2581  get_first_metadata = 1;
2582  sequence_change = 0;
2583  }
2584  }
2585 
2586  read_size_bytes = buf_info.buf_avail_size;
2588  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
2589  {
2590  read_size_bytes = ((read_size_bytes / NI_MEM_PAGE_ALIGNMENT) *
2593  }
2594 
2595  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
2596  p_data_buffer, read_size_bytes, ui32LBA);
2597  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
2598  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
2599  CHECK_VPU_RECOVERY(retval);
2600  if (retval < 0)
2601  {
2602  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
2604  LRETURN;
2605  } else if (get_first_metadata) {
2606  // got first metadata alone
2607  ni_metadata_dec_frame_t *p_meta =
2608  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
2609  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
2610  p_meta->metadata_common.pkt_delay_cnt);
2611  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2612  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2613  get_first_metadata = 0;
2614  ni_aligned_free(p_data_buffer);
2615  goto start;
2616  } else {
2617  // command issued successfully, now exit
2618  ni_metadata_dec_frame_t *p_meta;
2619  p_meta =
2620  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2621  p_frame->data_len[0] +
2622  p_frame->data_len[1] +
2623  p_frame->data_len[2]);
2624 
2625  if (buf_info.buf_avail_size != metadata_hdr_size)
2626  {
2627  low_delay_notify = 1;
2628  sei_size = p_meta->sei_size;
2629  } else if (ni_cmp_fw_api_ver(
2631  "6rE") >= 0)
2632  {
2633  p_meta =
2634  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
2635  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
2636  p_meta->metadata_common.pkt_delay_cnt);
2637  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
2638  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
2639  }
2640  total_bytes_to_read = total_bytes_to_read + sei_size;
2641  ni_log2(p_ctx, NI_LOG_DEBUG, "decoder read success, size %d total_bytes_to_read "
2642  "include sei %u sei_size %d\n",
2643  retval, total_bytes_to_read, sei_size);
2644  }
2645 
2646  bytes_read_so_far = total_bytes_to_read ;
2647  // Note: session status is NOT reset but tracked between send
2648  // and recv to catch and recover from a loop condition
2649 
2650  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
2651  p_ctx->frame_pkt_offset = frame_offset;
2652  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
2653  p_ctx->enable_low_delay_check)
2654  {
2655  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
2656 
2657  ni_metadata_dec_frame_t *p_meta =
2658  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
2659  p_frame->data_len[0] +
2660  p_frame->data_len[1] +
2661  p_frame->data_len[2]);
2662  if (p_meta->metadata_common.has_b_frame == 1)
2663  {
2664  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
2665  "is cancelled due to has_b_frames, frame_num %u\n",
2666  p_ctx->session_id, p_ctx->frame_num);
2667  p_ctx->decoder_low_delay = 0;
2668  }
2669  }
2670 
2671  if (rx_size > 0)
2672  {
2673  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
2674  p_ctx->session_run_state, p_ctx->is_first_frame);
2676  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2678  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2679  {
2680  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
2681  {
2682  p_ctx->pic_reorder_delay++;
2683  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
2684  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
2685  } else
2686  {
2687  p_frame->dts = NI_NOPTS_VALUE;
2688  }
2689  }
2690 
2691  if (p_ctx->is_first_frame)
2692  {
2693  for (i = 0; i < p_ctx->pic_reorder_delay; i++)
2694  {
2695  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
2696  p_ctx->last_dts == NI_NOPTS_VALUE)
2697  {
2698  // If the p_frame->pts is unknown in the very beginning we assume
2699  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
2700  if (p_frame->pts >= p_frame->dts &&
2701  p_frame->pts - p_frame->dts < 1000)
2702  {
2703  break;
2704  }
2705  }
2706 
2708  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
2710  p_ctx->frame_num % 500 == 0,
2711  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
2712  {
2713  p_frame->dts = NI_NOPTS_VALUE;
2714  }
2715  }
2716  // Reset for DTS padding counting
2717  p_ctx->pic_reorder_delay = 0;
2718  }
2719  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
2720  {
2721  if (p_ctx->is_first_frame)
2722  {
2723  p_ctx->is_first_frame = 0;
2724  }
2725  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
2726  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
2727  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
2728  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
2729  p_frame->pts, p_ctx->frame_num, p_frame->dts);
2730  }
2731  else if (p_ctx->is_dec_pkt_512_aligned)
2732  {
2733  if (p_ctx->is_first_frame)
2734  {
2735  p_ctx->is_first_frame = 0;
2736  p_frame->pkt_pos = p_ctx->pkt_pos[0];
2737 
2738  if (p_frame->dts == NI_NOPTS_VALUE)
2739  {
2740  p_frame->pts = NI_NOPTS_VALUE;
2741  }
2742  // if not a bitstream retrieve the pts of the frame corresponding to the first YUV output
2743  else if((p_ctx->pts_offsets[0] != NI_NOPTS_VALUE) && (p_ctx->pkt_index != -1))
2744  {
2745  ni_metadata_dec_frame_t* p_metadata =
2746  (ni_metadata_dec_frame_t*)((uint8_t*)p_frame->p_buffer +
2747  p_frame->data_len[0] + p_frame->data_len[1] +
2748  p_frame->data_len[2] + p_frame->data_len[3]);
2749  uint64_t num_fw_pkts =
2750  p_metadata->metadata_common.ui64_data.frame_offset / 512;
2751  ni_log2(p_ctx, NI_LOG_DEBUG,
2752  "%s: num_fw_pkts %" PRIu64 " frame_offset %" PRIu64 "\n",
2753  __func__, num_fw_pkts,
2754  p_metadata->metadata_common.ui64_data.frame_offset);
2755  int idx = 0;
2756  uint64_t cumul = p_ctx->pkt_offsets_index[0];
2757  bool bFound = (num_fw_pkts >= cumul);
2758  while (cumul < num_fw_pkts) // look for pts index
2759  {
2760  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: cumul %" PRIu64 "\n", __func__, cumul);
2761  if (idx == NI_MAX_DEC_REJECT)
2762  {
2763  ni_log2(p_ctx, NI_LOG_ERROR, "Invalid index computation > "
2764  "NI_MAX_DEC_REJECT!\n");
2765  break;
2766  } else
2767  {
2768  idx++;
2769  cumul += p_ctx->pkt_offsets_index[idx];
2770  ni_log2(p_ctx, NI_LOG_DEBUG,
2771  "%s: idx %d "
2772  "pkt_offsets_index[idx] %" PRIu64 "\n",
2773  __func__, idx, p_ctx->pkt_offsets_index[idx]);
2774  }
2775  }
2776  //if ((idx != NI_MAX_DEC_REJECT) && (idx >= 0))
2777  if ((idx != NI_MAX_DEC_REJECT) && bFound)
2778  {
2779  p_frame->pts = p_ctx->pts_offsets[idx];
2780  p_frame->flags = p_ctx->flags_array[idx];
2781  ni_log2(p_ctx, NI_LOG_DEBUG,
2782  "%s: (first frame) idx %d last_dts %" PRId64 ""
2783  "dts %" PRId64 " last_pts %" PRId64 " pts %" PRId64 "\n",
2784  __func__, idx, p_ctx->last_dts, p_frame->dts,
2785  p_ctx->last_pts, p_frame->pts);
2786  } else if ((idx != NI_MAX_DEC_REJECT) &&
2788  {
2789  ni_log2(p_ctx, NI_LOG_DEBUG,
2790  "%s(): session %u recovering and "
2791  "adjusting ts.\n",
2792  __func__, p_ctx->session_id);
2793  p_frame->pts = p_ctx->pts_offsets[idx];
2794  p_frame->flags = p_ctx->flags_array[idx];
2796  }
2797  else // use pts = 0 as offset
2798  {
2799  p_frame->pts = 0;
2800  ni_log2(p_ctx, NI_LOG_DEBUG,
2801  "%s: (zero default) dts %" PRId64 " pts "
2802  "%" PRId64 "\n",
2803  __func__, p_frame->dts, p_frame->pts);
2804  }
2805  }
2806  else
2807  {
2808  p_frame->pts = 0;
2809  ni_log2(p_ctx, NI_LOG_DEBUG,
2810  "%s: (not bitstream) dts %" PRId64 " pts "
2811  "%" PRId64 "\n",
2812  __func__, p_frame->dts, p_frame->pts);
2813  }
2814  }
2815  else
2816  {
2817  int64_t pts_delta = p_frame->dts - p_ctx->last_dts;
2818  p_frame->pts = p_ctx->last_pts + pts_delta;
2819  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
2820 
2821  ni_log2(p_ctx, NI_LOG_DEBUG,
2822  "%s: (!is_first_frame idx) last_dts %" PRId64 ""
2823  "dts %" PRId64 " pts_delta %" PRId64 " last_pts "
2824  "%" PRId64 " pts %" PRId64 "\n",
2825  __func__, p_ctx->last_dts, p_frame->dts, pts_delta,
2826  p_ctx->last_pts, p_frame->pts);
2827  }
2828  }
2829  else
2830  {
2831  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
2832  frame_offset);
2833  if (p_ctx->is_first_frame)
2834  {
2835  p_ctx->is_first_frame = 0;
2836  }
2837  // search for the pkt_offsets of received frame according to frame_offset.
2838  // here we get the index(i) which promises (p_ctx->pkt_offsets_index_min[i] <= frame_offset && p_ctx->pkt_offsets_index[i] > frame_offset)
2839  // i = -1 if not found
2840  i = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
2841  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
2842  frame_offset);
2843  if (i >= 0)
2844  {
2845  p_frame->pts = p_ctx->pts_offsets[i];
2846  p_frame->flags = p_ctx->flags_array[i];
2847  p_frame->pkt_pos = p_ctx->pkt_pos[i];
2848  ni_log2(p_ctx, NI_LOG_DEBUG,
2849  "%s: (found pts) dts %" PRId64 " pts "
2850  "%" PRId64 " frame_offset %" PRIu64 " i %d "
2851  "pkt_offsets_index_min %" PRIu64 " pkt_offsets_index "
2852  "%" PRIu64 " pkt_pos %" PRIu64 " \n",
2853  __func__, p_frame->dts, p_frame->pts, frame_offset, i,
2854  p_ctx->pkt_offsets_index_min[i],
2855  p_ctx->pkt_offsets_index[i],
2856  p_ctx->pkt_pos[i]);
2857 
2858  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[i];
2859  p_ctx->pkt_custom_sei_set[i] = NULL;
2860  } else
2861  {
2862  // backup solution pts
2863  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
2864  {
2865  // if skip guess_correct_pts, use pts interval to get the correct pts
2866  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
2867  }
2868  else
2869  {
2870  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
2871  }
2872  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
2873  ni_log2(p_ctx, NI_LOG_ERROR,
2874  "ERROR: Frame pts %" PRId64 " not found for offset "
2875  "%" PRIu64 "\n", p_frame->pts, frame_offset);
2876  ni_log2(p_ctx, NI_LOG_DEBUG,
2877  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
2878  "\n",
2879  __func__, p_frame->dts, p_frame->pts);
2880  }
2881  }
2882  p_frame->orignal_pts = p_frame->pts;
2883  p_ctx->last_pkt_pos = p_frame->pkt_pos;
2884  p_ctx->last_frame_offset = frame_offset;
2885  if (!p_param->dec_input_params.skip_pts_guess)
2886  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
2887  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
2888  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
2889  p_ctx->last_pts = p_frame->pts;
2890  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
2891  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
2892  p_ctx->last_dts = p_frame->dts;
2893  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
2894  __func__, p_frame->pts);
2895  p_ctx->frame_num++;
2896 
2897 #ifdef MEASURE_LATENCY
2898 #ifndef XCODER_311
2899  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
2900  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
2901 #endif
2902 #endif
2903 
2904 #ifdef XCODER_DUMP_DATA
2905  char dump_file[256];
2906  snprintf(dump_file, sizeof(dump_file), "%ld-%u-dec-fme/fme-%04ld.yuv",
2907  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
2908  FILE *f = fopen(dump_file, "wb");
2909  fwrite(p_frame->p_buffer,
2910  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
2911  1, f);
2912  fflush(f);
2913  fclose(f);
2914 #endif
2915  }
2916 
2917  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
2918  ni_log2(p_ctx, NI_LOG_DEBUG,
2919  "%s(): p_frame->start_of_stream=%u, "
2920  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
2921  "p_frame->video_height=%u\n",
2922  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
2923  p_frame->video_width, p_frame->video_height);
2924  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
2925  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
2926 
2927  if (p_ctx->frame_num % 500 == 0)
2928  {
2929  ni_log2(p_ctx, NI_LOG_DEBUG,
2930  "Decoder pts queue size = %u dts queue size = %u\n\n",
2931  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
2932  // scan and clean up
2934  p_ctx->buffer_pool);
2935  }
2936 
2937 #ifdef MEASURE_LATENCY
2938  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
2939  {
2940  uint64_t abs_time_ns = ni_gettime_ns();
2942  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
2943  p_frame->dts, abs_time_ns - q->last_benchmark_time,
2944  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
2945  q->last_benchmark_time = abs_time_ns;
2946  }
2947 #endif
2948 
2949 END:
2950 
2951  ni_pthread_mutex_unlock(&p_ctx->mutex);
2952 
2953  if (get_first_metadata && p_data_buffer)
2954  ni_aligned_free(p_data_buffer);
2955  if (sequence_change && p_ctx->frame_num)
2956  {
2957  if (p_ctx->actual_video_width == p_frame->video_width &&
2958  p_ctx->active_video_height == p_frame->video_height)
2959  {
2960  p_ctx->pixel_format_changed = 1;
2961  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
2962  }
2963  }
2964 
2965  if (NI_RETCODE_SUCCESS != retval)
2966  {
2967  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
2968  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
2969  {
2970  low_delay_signal(p_ctx);
2971  }
2972  return retval;
2973  } else
2974  {
2975  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
2976  if (low_delay_notify)
2977  {
2978  low_delay_signal(p_ctx);
2979  }
2980  return rx_size;
2981  }
2982 }
2983 
2984 /*!******************************************************************************
2985  * \brief Query current xcoder status
2986  *
2987  * \param
2988  *
2989  * \return
2990  *******************************************************************************/
2992  ni_device_type_t device_type)
2993 {
2995  int retval = NI_RETCODE_SUCCESS;
2996 
2997  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
2998  device_type, g_device_type_str[device_type]);
2999 
3000  if (!p_ctx)
3001  {
3002  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3003  __func__);
3004  retval = NI_RETCODE_INVALID_PARAM;
3005  LRETURN;
3006  }
3007 
3008  retval = ni_query_general_status(p_ctx, device_type, &data);
3009  if (NI_RETCODE_SUCCESS == retval)
3010  {
3014  p_ctx->overall_load_query.admin_queried = (uint32_t)data.admin_nsid;
3015  p_ctx->load_query.current_load =
3016  (uint32_t)data.process_load_percent | (uint32_t)(data.process_load_percent_upper << 8);
3017  p_ctx->load_query.fw_model_load = (uint32_t)data.fw_model_load;
3018  p_ctx->load_query.fw_load = (uint32_t)data.fw_load;
3020 
3021  switch (device_type)
3022  {
3025  case NI_DEVICE_TYPE_UPLOAD:
3026  if (p_ctx->load_query.fw_load == 1 &&
3028  {
3029  // ignore negligible non-video traffic
3030  p_ctx->load_query.fw_load -= 1;
3031  }
3032  break;
3033  default:
3034  break;
3035  }
3036  p_ctx->load_query.total_contexts = (uint32_t)data.active_sub_instances_cnt;
3037  p_ctx->load_query.fw_video_mem_usage = (uint32_t)data.fw_video_mem_usage;
3039  p_ctx->load_query.fw_share_mem_usage = (uint32_t)data.fw_share_mem_usage;
3040  p_ctx->load_query.fw_p2p_mem_usage = (uint32_t)data.fw_p2p_mem_usage;
3042  (uint32_t)data.active_hwupload_sub_inst_cnt;
3043  ni_log2(p_ctx, NI_LOG_DEBUG, "%s blk_dev %s blk_xcoder %s dev_xcoder %s current_load:%u model_load:%u fw_load:%u "
3044  "total_contexts:%u fw_video_mem_usage:%u "
3045  "fw_video_shared_mem_usage:%u fw_share_mem_usage:%u "
3046  "fw_p2p_mem_usage:%u active_hwuploaders:%u\n", __func__,
3047  p_ctx->blk_dev_name,
3048  p_ctx->blk_xcoder_name,
3049  p_ctx->dev_xcoder_name,
3050  p_ctx->load_query.current_load,
3051  p_ctx->load_query.fw_model_load,
3052  p_ctx->load_query.fw_load,
3053  p_ctx->load_query.total_contexts,
3059  if (p_ctx->overall_load_query.admin_queried)
3060  {
3061  ni_log2(p_ctx, NI_LOG_DEBUG,
3062  "Overall load %u, model load %u, instance count %u\n",
3066  }
3067  }
3068 
3069 END:
3070 
3071  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3072 
3073  return retval;
3074 }
3075 
3076 /*!******************************************************************************
3077  * \brief Query current xcoder status
3078  *
3079  * \param
3080  *
3081  * \return
3082  *******************************************************************************/
3084  ni_device_type_t device_type, void *detail_data, int ver)
3085 {
3086  int retval = NI_RETCODE_SUCCESS;
3087 
3088  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): device_type %d:%s; enter\n", __func__,
3089  device_type, g_device_type_str[device_type]);
3090 
3091  if (!p_ctx)
3092  {
3093  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3094  __func__);
3095  retval = NI_RETCODE_INVALID_PARAM;
3096  LRETURN;
3097  }
3098 
3099  retval = ni_query_detail_status(p_ctx, device_type, detail_data, ver);
3100 
3101 END:
3102 
3103  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3104 
3105  return retval;
3106 }
3107 
3108 /*!******************************************************************************
3109  * \brief Open a xcoder encoder instance
3110  *
3111  * \param
3112  *
3113  * \return
3114  *******************************************************************************/
3115 
3117 {
3119  ni_xcoder_params_t *p_param;
3120  void *p_buffer = NULL;
3121  ni_instance_buf_info_t buf_info = {0};
3122  uint32_t ui32LBA = 0;
3123  uint32_t max_cu_size;
3124  uint32_t block_size;
3125  int32_t width, height;
3126  int m_threshold = 0;
3127  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
3128 
3129  if (!p_ctx || !p_ctx->p_session_config)
3130  {
3131  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3132  __func__);
3133  retval = NI_RETCODE_INVALID_PARAM;
3134  LRETURN;
3135  }
3136 
3137  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3138  bool isrgba = (p_ctx->pixel_format == NI_PIX_FMT_ABGR || p_ctx->pixel_format == NI_PIX_FMT_ARGB
3139  || p_ctx->pixel_format == NI_PIX_FMT_RGBA || p_ctx->pixel_format == NI_PIX_FMT_BGRA);
3140  if (isrgba)
3141  {
3143  || p_param->source_width < NI_MIN_WIDTH || p_param->source_height < NI_MIN_HEIGHT)
3144  {
3145  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() RGBA / BGRA / ARGB / AGBR resolution invalid, return\n",
3146  __func__);
3147  retval = NI_RETCODE_INVALID_PARAM;
3148  LRETURN;
3149  }
3150  }
3151 
3152  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hwframes = %d\n", __func__,
3153  p_param->hwframes);
3154 
3155  // calculate encoder ROI map size: each QP info takes 8-bit, represent 8 x 8
3156  // pixel block
3157  max_cu_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
3158 
3159  width = p_param->source_width;
3160  height = p_param->source_height;
3161  // AV1 non-8x8-aligned resolution is implicitly cropped due to Quadra HW limitation
3162  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
3163  {
3164  width = (width / 8) * 8;
3165  height = (height / 8) * 8;
3166  }
3167 
3168  block_size =
3169  ((width + max_cu_size - 1) & (~(max_cu_size - 1))) *
3170  ((height + max_cu_size - 1) & (~(max_cu_size - 1))) /
3171  (8 * 8);
3172  p_ctx->roi_len = ((block_size + 63) & (~63)); // align to 64 bytes
3173 
3174  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): finish init\n", __func__);
3175 
3176  //Check if there is an instance or we need a new one
3177  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3178  {
3180  p_ctx->pts_table = NULL;
3181  p_ctx->dts_queue = NULL;
3182  p_ctx->buffer_pool = NULL;
3183  p_ctx->status = 0;
3184  p_ctx->key_frame_type = 0;
3185  p_ctx->keyframe_factor = 1;
3186  p_ctx->frame_num = 0;
3187  p_ctx->pkt_num = 0;
3188  p_ctx->av1_pkt_num = 0;
3189  p_ctx->rc_error_count = 0;
3190  p_ctx->force_frame_type = 0;
3191  p_ctx->ready_to_close = 0;
3192  p_ctx->auto_dl_handle = 0;
3193  //Sequence change tracking related stuff
3194  p_ctx->active_video_width = 0;
3195  p_ctx->active_video_height = 0;
3196  p_ctx->p_all_zero_buf = NULL;
3197  p_ctx->actual_video_width = 0;
3198  p_ctx->enc_pts_w_idx = 0;
3199  p_ctx->enc_pts_r_idx = 0;
3200  p_ctx->session_timestamp = 0;
3201  memset(p_ctx->pkt_custom_sei_set, 0, NI_FIFO_SZ * sizeof(ni_custom_sei_set_t *));
3202  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
3204  {
3206  }
3207 
3208  //malloc zero data buffer
3209  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
3211  {
3212  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
3213  NI_ERRNO, __func__);
3214  retval = NI_RETCODE_ERROR_MEM_ALOC;
3215  LRETURN;
3216  }
3217  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
3218 
3219  //malloc data buffer
3220  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3221  {
3222  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3223  NI_ERRNO, __func__);
3225  retval = NI_RETCODE_ERROR_MEM_ALOC;
3226  LRETURN;
3227  }
3228  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3229 
3230  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
3231  //In case we can open sesison, the session id would become valid.
3232  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
3233  (uint16_t)NI_INVALID_SESSION_ID;
3234 
3235  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
3237  ni_htonl(p_ctx->codec_format), 0);
3238  ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer,
3239  NI_DATA_BUFFER_LEN, ui32LBA);
3240  //Open will return a session status structure with a valid session id if it worked.
3241  //Otherwise the invalid session id set before the open command will stay
3242  p_ctx->session_id =
3243  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
3244  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
3245  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
3246  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
3247  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3248  {
3249  ni_log2(p_ctx, NI_LOG_ERROR,
3250  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3251  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3252  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3253  p_ctx->session_id);
3254  ni_encoder_session_close(p_ctx, 0);
3256  LRETURN;
3257  }
3258  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder open session ID:0x%x timestamp:%" PRIu64 "\n",
3259  p_ctx->session_id, p_ctx->session_timestamp);
3260 
3261  //Send keep alive timeout Info
3262  uint64_t keep_alive_timeout =
3263  p_ctx->keep_alive_timeout * 1000000; //send us to FW
3264  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3265  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
3266  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
3267  keep_alive_timeout);
3269  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3270  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3271  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3272  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3273  CHECK_VPU_RECOVERY(retval);
3274 
3275  if (NI_RETCODE_SUCCESS != retval)
3276  {
3277  ni_log2(p_ctx, NI_LOG_ERROR,
3278  "ERROR %s(): nvme write keep_alive_timeout command "
3279  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3280  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3282  LRETURN;
3283  }
3284 
3285  // Send SW version to FW if FW API version is >= 6.2
3287  "62") >= 0)
3288  {
3289  // Send SW version to session manager
3290  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3291  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
3293  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
3294  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
3295  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
3296  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
3297  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3298  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3299  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
3300  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
3301  CHECK_VPU_RECOVERY(retval);
3302 
3303  if (NI_RETCODE_SUCCESS != retval)
3304  {
3305  ni_log2(p_ctx, NI_LOG_ERROR,
3306  "ERROR %s(): nvme write sw_version command "
3307  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
3308  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
3310  LRETURN;
3311  }
3312 
3313  if (ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0)
3314  {
3315  // For FW API ver 6rc or newer, initialize with the most current size
3316  p_ctx->meta_size = sizeof(ni_metadata_enc_bstream_t);
3317  }
3319  "6p") >= 0)
3320  // For FW API ver 6.p or newer, initialize with the most current size
3322  else
3323  // For FW API ver 6.2 or newer, initialize with the most current size
3325  }
3326  else
3327  {
3328  // For FW API ver 6.1 or older, initialize with metadata size 32
3330  }
3331 
3332  // Open AI session for AI Enhance
3333  if(p_param->enable_ai_enhance)
3334  {
3335  // Check if the bit depth & resoultion
3336  if((p_ctx->bit_depth_factor != 1) ||
3337  ((p_param->source_width != 1280 && p_param->source_height != 720) &&
3338  (p_param->source_width != 1920 && p_param->source_height != 1080) &&
3339  (p_param->source_width != 3840 && p_param->source_height != 2160) &&
3340  (p_param->source_width != 720 && p_param->source_height != 1280) &&
3341  (p_param->source_width != 768 && p_param->source_height != 1280) &&
3342  (p_param->source_width != 1080 && p_param->source_height != 1920) &&
3343  (p_param->source_width != 2496 && p_param->source_height != 1080)))
3344  {
3345  p_param->enable_ai_enhance = 0;
3346  ni_log2(p_ctx, NI_LOG_ERROR, "Resoultion=%dx%d,depth=%d\n",p_param->source_width,p_param->source_height,p_ctx->bit_depth_factor);
3347  ni_log2(p_ctx, NI_LOG_ERROR, "The bit depth or resoultion is not supported on ai enhance mode\n");
3348  }else{
3349  ni_log2(p_ctx, NI_LOG_DEBUG, "Adding ai enhance session to encoder\n");
3350  ui32LBA = OPEN_ADD_CODEC(NI_DEVICE_TYPE_AI, 0, ni_htons(p_ctx->session_id));
3351  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
3352  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
3353 
3354  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_open,
3355  p_ctx->device_type, p_ctx->hw_id,
3356  &(p_ctx->session_id), OPT_1);
3357  if (NI_RETCODE_SUCCESS != retval)
3358  {
3359  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR couldn't add ai to encoder session\n");
3360  ni_log2(p_ctx, NI_LOG_ERROR,
3361  "ERROR %s(): p_ctx->device_handle=%" PRIx64 ", "
3362  "p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3363  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3364  p_ctx->session_id);
3365  ni_encoder_session_close(p_ctx, 0);
3366  LRETURN;
3367  }
3368  }
3369  }
3370 
3371  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3372  {
3373  retval = ni_device_set_ddr_configuration(p_ctx, p_param->ddr_priority_mode);
3374  if (NI_RETCODE_SUCCESS != retval)
3375  {
3376  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3377  __func__, strerror(NI_ERRNO));
3378  LRETURN;
3379  }
3380  }
3381 
3382  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
3383  }
3384 
3385  /* Modify mmap_threshold and trim_threshold of malloc */
3386  // calculate the default threshold which equals to the resolution + padding size.
3387  // 64 is AV_INPUT_BUFFER_PADDING_SIZE
3388  m_threshold = p_param->source_width * p_param->source_height * 3 / 2 *
3389  p_ctx->bit_depth_factor + 64;
3390  m_threshold = ((m_threshold + (NI_MEM_PAGE_ALIGNMENT - 1)) /
3392 
3393  // The upper limit is DEFAULT_MMAP_THRESHOLD_MAX: 512*1024 on 32-bit systems,
3394  // or 4*1024*1024*sizeof(long) on 64-bit systems.
3395  // This macro is not defined in header file.
3396  // So, assume that is running on 64-bit systems
3397  if (m_threshold > 4 * 1024 * 1024 * sizeof(long))
3398  {
3399  ni_log2(p_ctx, NI_LOG_INFO, "Warning: m_threshold (%d) is bigger than "
3400  "DEFAULT_MMAP_THRESHOLD_MAX, use default value (%d)\n",
3401  m_threshold, 4 * 1024 * 1024 * sizeof(long));
3402  m_threshold = 4 * 1024 * 1024 * sizeof(long);
3403  }
3404 
3405  if (p_param->staticMmapThreshold) // Set Static Mmap Threshold
3406  {
3407 #if defined(__linux__) && !defined(_ANDROID)
3408  // If the malloc buffer is larger than the threshold,
3409  // glibc will use mmap to malloc the memory, which is a low speed method.
3410  // So, set the M_MMAP_THRESHOLD >= 1 yuv buffer size here.
3411  if (mallopt(M_MMAP_THRESHOLD, m_threshold) == 0)
3412  {
3413  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_MMAP_THRESHOLD to %d\n",
3414  m_threshold);
3415  retval = NI_RETCODE_ERROR_MEM_ALOC;
3416  LRETURN;
3417  }
3418 
3419  // When the amount of contiguous free memory at the top of the heap
3420  // grows larger than M_TRIM_THRESHOLD,
3421  // free(3) employs sbrk(2) to release this memory back to the system.
3422  // So, set the M_TRIM_THRESHOLD to 3*m_threshold,
3423  // to avoid an inefficient case, frequently releasing and requesting physical memory.
3424  if (mallopt(M_TRIM_THRESHOLD, 3 * m_threshold) == 0)
3425  {
3426  ni_log2(p_ctx, NI_LOG_ERROR, "Error: could not set M_TRIM_THRESHOLD to %ds\n",
3427  3 * m_threshold);
3428  retval = NI_RETCODE_ERROR_MEM_ALOC;
3429  LRETURN;
3430  }
3431 #else
3432  ni_log2(p_ctx, NI_LOG_INFO,
3433  "Warning: this platform does not support staticMmapThreshold\n");
3434 #endif
3435  }
3436  else // default
3437  {
3438  // a way to dynamically trigger a pre-mature increase in mmap&trim threshold
3439  // make the mmap_threshold larger than frame size to avoid frequent page faults
3440  m_threshold = (int) (m_threshold * 1.25); // An experience value
3441  void *temp_buf = malloc((size_t) m_threshold);
3442  ni_log2(p_ctx, NI_LOG_TRACE, "trigger a pre-mature increase in mmap&trim threshold: 0x%p = malloc(%d)\n",
3443  temp_buf, m_threshold);
3444  free(temp_buf);
3445  }
3446 
3447  if (p_ctx->hw_action == NI_CODEC_HW_ENABLE)
3448  {
3449  ni_device_capability_t sender_cap, receiver_cap;
3450  g_device_in_ctxt = true;
3451  ni_device_handle_t temp = g_dev_handle;
3452  g_dev_handle = p_ctx->sender_handle;
3453  retval = ni_device_capability_query(p_ctx->sender_handle, &sender_cap);
3454  if (retval != NI_RETCODE_SUCCESS)
3455  {
3456  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query returned %d\n",
3457  retval);
3458  LRETURN;
3459  }
3460  g_dev_handle = p_ctx->blk_io_handle;
3461  retval = ni_device_capability_query(p_ctx->blk_io_handle, &receiver_cap);
3462 
3463  if (retval != NI_RETCODE_SUCCESS)
3464  {
3465  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_device_capability_query returned %d\n",
3466  retval);
3467  LRETURN;
3468  }
3469  g_dev_handle = temp;
3470  g_device_in_ctxt = false; //cleanup
3471  for (uint8_t ui8Index = 0; ui8Index < 20; ui8Index++)
3472  {
3473  if (sender_cap.serial_number[ui8Index] !=
3474  receiver_cap.serial_number[ui8Index])
3475  {
3476  // QDFW-315 Autodownload
3477  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3478  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3479  p_ctx->auto_dl_handle);
3480  p_ctx->hw_action = NI_CODEC_HW_NONE;
3481  break;
3482  }
3483  else
3484  {
3486  "6m") >= 0)
3487  {
3488  ni_device_vf_ns_id_t sender_vf_ns_id = {0};
3489  ni_device_vf_ns_id_t curr_vf_ns_id = {0};
3490  retval = ni_query_vf_ns_id(p_ctx->sender_handle, &sender_vf_ns_id, p_ctx->fw_rev);
3491  if (retval != NI_RETCODE_SUCCESS)
3492  {
3493  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3494  "p_ctx->sender_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3495  (int64_t)p_ctx->sender_handle, p_ctx->hw_id, p_ctx->session_id);
3496  ni_encoder_session_close(p_ctx, 0);
3497  LRETURN;
3498  }
3499  retval = ni_query_vf_ns_id(p_ctx->blk_io_handle, &curr_vf_ns_id, p_ctx->fw_rev);
3500  if (retval != NI_RETCODE_SUCCESS)
3501  {
3502  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_query_vf_ns_id(): "
3503  "p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%u\n",
3504  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
3505  ni_encoder_session_close(p_ctx, 0);
3506  LRETURN;
3507  }
3508 
3509  if ((sender_vf_ns_id.ns_id != curr_vf_ns_id.ns_id) ||
3510  (sender_vf_ns_id.vf_id != curr_vf_ns_id.vf_id))
3511  {
3512  p_ctx->auto_dl_handle = p_ctx->sender_handle;
3513  ni_log2(p_ctx, NI_LOG_DEBUG, "Autodownload device handle set %p!\n",
3514  p_ctx->auto_dl_handle);
3515  p_ctx->hw_action = NI_CODEC_HW_NONE;
3516  break;
3517  }
3518  }
3519  }
3520  }
3521  }
3522 
3523  retval = ni_config_instance_set_encoder_params(p_ctx);
3524  if (NI_RETCODE_SUCCESS != retval)
3525  {
3526  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: calling ni_config_instance_set_encoder_params(): "
3527  "p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
3528  (int64_t)p_ctx->device_handle, p_ctx->hw_id, p_ctx->session_id);
3529  ni_encoder_session_close(p_ctx, 0);
3530  LRETURN;
3531  }
3532  if(p_param->enable_ai_enhance)
3533  {
3534  retval = ni_ai_query_network_ready(p_ctx);
3535  if(retval != NI_RETCODE_SUCCESS)
3536  {
3537  ni_log2(p_ctx, NI_LOG_ERROR,
3538  "ERROR: ni_ai_query_network_ready returned %d\n", retval);
3539  LRETURN;
3540  }
3541  }
3542 
3543  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "enc_pts");
3544  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "enc_dts");
3545 
3546  // init close caption SEI header and trailer
3547  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_hevc, g_itu_t_t35_cc_sei_hdr_hevc,
3549  memcpy(p_ctx->itu_t_t35_cc_sei_hdr_h264, g_itu_t_t35_cc_sei_hdr_h264,
3551  memcpy(p_ctx->sei_trailer, g_sei_trailer, NI_CC_SEI_TRAILER_LEN);
3552  // init hdr10+ SEI header
3553  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_hevc, g_itu_t_t35_hdr10p_sei_hdr_hevc,
3555  memcpy(p_ctx->itu_t_t35_hdr10p_sei_hdr_h264, g_itu_t_t35_hdr10p_sei_hdr_h264,
3557 
3558  // query to check the final encoder config status
3559  for (;;)
3560  {
3562  NI_DEVICE_TYPE_ENCODER, &buf_info);
3563 
3564  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3565  p_ctx->device_type, p_ctx->hw_id,
3566  &(p_ctx->session_id), OPT_1);
3567 
3569  || NI_RETCODE_FAILURE == retval)
3570  {
3571  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d return %d\n", __func__, __LINE__, retval);
3572  LRETURN;
3573  }
3574  else if (buf_info.buf_avail_size > 0)
3575  {
3576  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): buf_avail_size %u\n", __func__,
3577  buf_info.buf_avail_size);
3578  break;
3579  }
3580  else
3581  {
3582  ni_usleep(1000);
3583  }
3584  }
3585 
3586  ni_log2(p_ctx, NI_LOG_DEBUG,
3587  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3588  "p_ctx->session_id=%d\n",
3589  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3590  p_ctx->session_id);
3591 
3592 #ifdef XCODER_DUMP_DATA
3593  char dir_name[256] = {0};
3594  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-pkt", (long)getpid(),
3595  p_ctx->session_id);
3596  DIR *dir = opendir(dir_name);
3597  if (!dir && ENOENT == NI_ERRNO)
3598  {
3599  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3600  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder pkt dump dir created: %s\n", dir_name);
3601  }
3602 
3603  if(dir){
3604  closedir(dir);
3605  }
3606 
3607  snprintf(dir_name, sizeof(dir_name), "%ld-%u-enc-fme", (long)getpid(),
3608  p_ctx->session_id);
3609  dir = opendir(dir_name);
3610  if (!dir && ENOENT == NI_ERRNO)
3611  {
3612  mkdir(dir_name, S_IRWXU | S_IRWXG | S_IRWXO);
3613  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder frame dump dir created: %s\n", dir_name);
3614  }
3615  if(dir){
3616  closedir(dir);
3617  }
3618 #endif
3619 
3620 END:
3621 
3622  ni_aligned_free(p_buffer);
3623  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3624 
3625  return retval;
3626 }
3627 
3628 /*!******************************************************************************
3629  * \brief Flush encoder output
3630  *
3631  * \param
3632  *
3633  * \return
3634  *******************************************************************************/
3636 {
3637  ni_retcode_t retval;
3638  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3639  if (!p_ctx)
3640  {
3641  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3642  __func__);
3643  retval = NI_RETCODE_INVALID_PARAM;
3644  LRETURN;
3645  }
3646 
3647  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3648  {
3649  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
3650  __func__);
3652  LRETURN;
3653  }
3654 
3656  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
3657  p_ctx->device_type, p_ctx->hw_id,
3658  &(p_ctx->session_id), OPT_1);
3659  CHECK_VPU_RECOVERY(retval);
3660 
3661 END:
3662 
3663  if (NI_RETCODE_SUCCESS != retval)
3664  {
3665  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): %d, return\n", __func__, retval);
3666  }
3667 
3668  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3669 
3670  return retval;
3671 }
3672 
3673 /*!******************************************************************************
3674  * \brief Close a xcoder encoder instance
3675  *
3676  * \param
3677  *
3678  * \return
3679  *******************************************************************************/
3681 {
3683  void* p_buffer = NULL;
3684  uint32_t ui32LBA = 0;
3685  int i = 0;
3686  ni_xcoder_params_t *p_param = NULL;
3687 
3688  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
3689 
3690  if (!p_ctx)
3691  {
3692  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3693  __func__);
3694  return NI_RETCODE_INVALID_PARAM;
3695  }
3696 
3697  ni_pthread_mutex_lock(&p_ctx->mutex);
3698 
3699  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3700  {
3701  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
3702  retval = NI_RETCODE_SUCCESS;
3703  LRETURN;
3704  }
3705 
3706  if (p_ctx->device_type == NI_DEVICE_TYPE_ENCODER)
3707  {
3708  ni_log2(p_ctx, NI_LOG_INFO,
3709  "Encoder_complete_info:session_id 0x%x, total frames input:%u "
3710  "buffered: %u completed: %u output: %u dropped: %u error: %u\n",
3717  }
3718 
3719  //malloc data buffer
3720  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
3721  {
3722  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
3723  NI_ERRNO, __func__);
3724  retval = NI_RETCODE_ERROR_MEM_ALOC;
3725  LRETURN;
3726  }
3727  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
3728 
3729  if (p_ctx->p_session_config)
3730  {
3731  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
3732  if (p_param->ddr_priority_mode > NI_DDR_PRIORITY_NONE)
3733  {
3735  if (NI_RETCODE_SUCCESS != retval)
3736  {
3737  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): ddr priority setting failure for %s\n",
3738  __func__, strerror(NI_ERRNO));
3739  LRETURN;
3740  }
3741  }
3742  }
3743 
3745 
3746  int retry = 0;
3747  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
3748  {
3749  ni_log2(p_ctx, NI_LOG_DEBUG,
3750  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
3751  "p_ctx->session_id=%d, close_mode=1\n",
3752  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
3753  p_ctx->session_id);
3754 
3756  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
3757  {
3758  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
3761  break;
3762  } else
3763  {
3764  //Close should always succeed
3765  retval = NI_RETCODE_SUCCESS;
3767  break;
3768  }
3769  /*
3770  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
3771  {
3772  retval = NI_RETCODE_SUCCESS;
3773  p_ctx->session_id = NI_INVALID_SESSION_ID;
3774  break;
3775  }
3776  else
3777  {
3778  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n");
3779  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
3780  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
3781  }
3782  */
3783  retry++;
3784  }
3785 
3786 END:
3787 
3788  ni_aligned_free(p_buffer);
3790 
3791  //Sequence change related stuff cleanup here
3792  p_ctx->active_video_width = 0;
3793  p_ctx->active_video_height = 0;
3794  p_ctx->actual_video_width = 0;
3795  //End of sequence change related stuff cleanup
3796 
3797  if (p_ctx->pts_table)
3798  {
3799  ni_queue_free(&p_ctx->pts_table->list, p_ctx->buffer_pool);
3800  ni_memfree(p_ctx->pts_table);
3801  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
3802  }
3803 
3804  if (p_ctx->dts_queue)
3805  {
3806  ni_queue_free(&p_ctx->dts_queue->list, p_ctx->buffer_pool);
3807  ni_memfree(p_ctx->dts_queue);
3808  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_timestamp_done: success\n");
3809  }
3810 
3812  p_ctx->buffer_pool = NULL;
3813 
3814  for (i = 0; i < NI_FIFO_SZ; i++)
3815  {
3816  ni_memfree(p_ctx->pkt_custom_sei_set[i]);
3817  }
3818 
3819  for (i = 0; i < 120 ; i++)
3820  {
3821  if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL)
3822  {
3823  free(p_ctx->input_frame_fifo[i].p_input_buffer);
3824  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
3825  }
3826  }
3827 
3828  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
3829  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
3830  p_ctx->session_id);
3831  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3832 
3833  low_delay_signal(p_ctx);
3834  ni_pthread_mutex_unlock(&p_ctx->mutex);
3835 
3836  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
3837 
3838  return retval;
3839 }
3840 
3841 
3843 {
3844  int32_t width_stride[4] = {0};
3845  int32_t height_stride[4] = {0};
3846  int32_t source_width = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->source_width;
3847  int32_t source_height = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->source_height;
3848 
3849  if (((ni_xcoder_params_t *)(p_ctx->p_session_config))->zerocopy_mode &&
3850  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize != 0 &&
3851  ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize != 0)
3852  {
3853  //zero copy happend
3854  width_stride[0] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->luma_linesize;
3855  width_stride[1] = width_stride[2] = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->chroma_linesize;
3856  }
3857  else
3858  {
3859  int is_semiplanar = !ni_get_planar_from_pixfmt(p_ctx->pixel_format);
3860  ni_get_hw_yuv420p_dim(ni_frame->video_width, ni_frame->video_height,
3861  p_ctx->bit_depth_factor, is_semiplanar,
3862  width_stride, height_stride);
3863  }
3864 
3865  for (int i = 0; i < 120; i++)
3866  {
3867  // ni_log2(p_ctx, NI_LOG_ERROR, "%s %d i %d frame_num %d p_input_buffer %p data_len[0:2] %d %d %d p_data[0:2] %p %p %p video_width %d video_height %d\n",
3868  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, p_ctx->input_frame_fifo[i].p_input_buffer,
3869  // ni_frame->data_len[0], ni_frame->data_len[1], ni_frame->data_len[2],
3870  // ni_frame->p_data[0], ni_frame->p_data[1], ni_frame->p_data[2],
3871  // ni_frame->video_width, ni_frame->video_height);
3872  if ((source_width != p_ctx->input_frame_fifo[i].video_width ||
3873  source_height != p_ctx->input_frame_fifo[i].video_height) &&
3874  p_ctx->input_frame_fifo[i].p_input_buffer != NULL &&
3875  p_ctx->input_frame_fifo[i].usable == 1)
3876  {
3877  free(p_ctx->input_frame_fifo[i].p_input_buffer);
3878  p_ctx->input_frame_fifo[i].p_input_buffer = NULL;
3879  p_ctx->input_frame_fifo[i].usable = -1;
3880  }
3881 
3882  if (p_ctx->input_frame_fifo[i].p_input_buffer == NULL)
3883  {
3884  p_ctx->input_frame_fifo[i].p_input_buffer = (uint8_t *)malloc(source_width * p_ctx->bit_depth_factor * source_height * 3 / 2);
3885 
3886  for (int j = 0; j < source_height; j++)
3887  {
3888  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
3889  ni_frame->p_data[0] + j * width_stride[0],
3890  source_width * p_ctx->bit_depth_factor);
3891  }
3892  for (int j = 0; j < (source_height + 1) / 2; j++)
3893  {
3894  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height + (source_width * p_ctx->bit_depth_factor / 2) * j,
3895  ni_frame->p_data[1] + j * width_stride[1],
3896  source_width / 2 * p_ctx->bit_depth_factor);
3897  }
3898  for (int j = 0; j < (source_height + 1) / 2; j++)
3899  {
3900  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height * 5 / 4 + (source_width * p_ctx->bit_depth_factor / 2) * j,
3901  ni_frame->p_data[2] + j * width_stride[2],
3902  source_width / 2 * p_ctx->bit_depth_factor);
3903  }
3904 
3905  p_ctx->input_frame_fifo[i].video_width = source_width;
3906  p_ctx->input_frame_fifo[i].video_height = source_height;
3907 
3908  p_ctx->input_frame_fifo[i].usable = -1;
3909  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
3910  // ni_log2(p_ctx, NI_LOG_DEBUG, "%s %d i %d frame_num %d buffer_size %d video_width %d video_height %d pts %lld\n",
3911  // __FUNCTION__, __LINE__, i, p_ctx->frame_num, ni_frame->buffer_size, ni_frame->video_width, ni_frame->video_height,
3912  // p_ctx->input_frame_fifo[i].pts);
3913  break;
3914  }
3915  else if (p_ctx->input_frame_fifo[i].p_input_buffer != NULL && p_ctx->input_frame_fifo[i].usable == 1)
3916  {
3917  for (int j = 0; j < source_height; j++)
3918  {
3919  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * j,
3920  ni_frame->p_data[0] + j * width_stride[0],
3921  source_width * p_ctx->bit_depth_factor);
3922  }
3923  for (int j = 0; j < (source_height + 1) / 2; j++)
3924  {
3925  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height + (source_width * p_ctx->bit_depth_factor / 2) * j,
3926  ni_frame->p_data[1] + j * width_stride[1],
3927  source_width / 2 * p_ctx->bit_depth_factor);
3928  }
3929  for (int j = 0; j < (source_height + 1) / 2; j++)
3930  {
3931  memcpy(p_ctx->input_frame_fifo[i].p_input_buffer + source_width * p_ctx->bit_depth_factor * source_height * 5 / 4 + (source_width * p_ctx->bit_depth_factor / 2) * j,
3932  ni_frame->p_data[2] + j * width_stride[2],
3933  source_width / 2 * p_ctx->bit_depth_factor);
3934  }
3935 
3936  p_ctx->input_frame_fifo[i].video_width = source_width;
3937  p_ctx->input_frame_fifo[i].video_height = source_height;
3938 
3939  p_ctx->input_frame_fifo[i].usable = -1;
3940  p_ctx->input_frame_fifo[i].pts = ni_frame->pts;
3941  break;
3942  }
3943  }
3944 }
3945 
3946 /*!******************************************************************************
3947  * \brief Send a YUV p_frame to encoder
3948  *
3949  * \param
3950  *
3951  * \return
3952  *******************************************************************************/
3954 {
3955  bool ishwframe = false;
3956  uint32_t size = 0;
3957  uint32_t send_count = 0;
3958  uint32_t i = 0;
3959  uint32_t sent_size = 0;
3960  uint32_t frame_size_bytes = 0;
3961  int retval = 0;
3962  ni_instance_buf_info_t buf_info = { 0 };
3963  ni_session_statistic_t sessionStatistic = {0};
3964 
3965  if (!p_ctx || !p_frame)
3966  {
3967  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
3968  __func__);
3969  return NI_RETCODE_INVALID_PARAM;
3970  }
3971  ishwframe = p_ctx->hw_action & NI_CODEC_HW_ENABLE;
3972  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter hw=%d\n", __func__, ishwframe);
3973 
3974  if (ishwframe && !p_frame->end_of_stream)
3975  {
3976  // check if the hw input frame is valid
3977  uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
3978  if (!((input_frame_idx > 0 &&
3979  input_frame_idx < NI_GET_MAX_HWDESC_FRAME_INDEX(p_ctx->ddr_config)) ||
3980  (input_frame_idx > NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) &&
3981  input_frame_idx <= NI_GET_MAX_HWDESC_P2P_BUF_ID(p_ctx->ddr_config))))
3982  {
3983  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): got invalid frameIdx [%u]\n",
3984  __func__, input_frame_idx);
3985  return NI_RETCODE_INVALID_PARAM;
3986  }
3987  }
3988 
3989  ni_pthread_mutex_lock(&p_ctx->mutex);
3990 
3991  uint8_t separate_metadata = p_frame->separate_metadata;
3992 
3993  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
3994 
3995  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
3996  {
3997  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
3998  __func__);
4000  LRETURN;
4001  }
4002 
4003  low_delay_wait(p_ctx);
4004 
4005 #ifdef MEASURE_LATENCY
4006  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
4007  {
4008  uint64_t abs_time_ns = ni_gettime_ns();
4009  ni_lat_meas_q_t *frame_time_q = (ni_lat_meas_q_t *)p_ctx->frame_time_q;
4010  ni_lat_meas_q_add_entry(frame_time_q, abs_time_ns, p_frame->dts);
4011  }
4012 #endif
4013 
4014  /*!********************************************************************/
4015  /*!************ Sequence Change related stuff *************************/
4016  //First check squence changed related stuff.
4017  //We need to record the current hight/width params if we didn't do it before:
4018 
4019  if( p_frame->video_height)
4020  {
4021  p_ctx->active_video_width = p_frame->data_len[0] / p_frame->video_height;
4022  p_ctx->active_video_height = p_frame->video_height;
4023  }
4024  else if (p_frame->video_width)
4025  {
4026  ni_log2(p_ctx, NI_LOG_DEBUG, "WARNING: passed video_height is not valid!, return\n");
4027  p_ctx->active_video_height = p_frame->data_len[0] / p_frame->video_width;
4028  p_ctx->active_video_width = p_frame->video_width;
4029  }
4030  else
4031  {
4032  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: passed video_height and video_width are not valid!, return\n");
4034  LRETURN;
4035  }
4036  // record actual width (in pixels / without padding) for sequnce change detection
4037  p_ctx->actual_video_width = p_frame->video_width;
4038 
4039  /*!************ Sequence Change related stuff end*************************/
4040  /*!********************************************************************/
4041 
4042  frame_size_bytes = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + p_frame->data_len[3] + p_frame->extra_data_len;
4043  ni_log2(p_ctx, NI_LOG_DEBUG,
4044  "%s: data_len[0] %u data_len[1] %u "
4045  "data_len[2] %u extra_data_len %u frame_size_bytes %u\n",
4046  __func__, p_frame->data_len[0], p_frame->data_len[1],
4047  p_frame->data_len[2], p_frame->extra_data_len, frame_size_bytes);
4048 
4049  // skip query write buffer because we just send EOS
4050  if (!p_frame->end_of_stream)
4051  {
4052  for (;;)
4053  {
4054  query_sleep(p_ctx);
4055 
4057  "65") >= 0)
4058  {
4060  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4061  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4063  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4064  CHECK_VPU_RECOVERY(retval);
4065  buf_info.buf_avail_size = sessionStatistic.ui32WrBufAvailSize;
4066  } else
4067  {
4068  retval =
4070  NI_DEVICE_TYPE_ENCODER, &buf_info);
4071  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4072  p_ctx->device_type, p_ctx->hw_id,
4073  &(p_ctx->session_id), OPT_1);
4074  CHECK_VPU_RECOVERY(retval);
4075  }
4076 
4077  if (NI_RETCODE_FAILURE == retval)
4078  {
4079  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() line-%d retrun %d\n", __func__, __LINE__, retval);
4080  LRETURN;
4081  }
4082  else if (NI_RETCODE_SUCCESS != retval ||
4083  buf_info.buf_avail_size < frame_size_bytes)
4084  {
4085  ni_log2(p_ctx, NI_LOG_TRACE,
4086  "Enc write query retry %d. rc=%d. Available buf size %u < "
4087  "frame size %u\n", retval, send_count,
4088  buf_info.buf_avail_size, frame_size_bytes);
4089  if (send_count >= NI_MAX_ENCODER_QUERY_RETRIES)
4090  {
4091  int retval_backup = retval;
4092  retval = ni_query_instance_buf_info(
4095  {
4096  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4097  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4098  CHECK_VPU_RECOVERY(retval);
4099  }
4100  else
4101  retval = NI_RETCODE_SUCCESS;
4102 
4103  ni_log2(p_ctx, NI_LOG_DEBUG,
4104  "Enc write query buf info exceeded max retries: "
4105  "%d, rc=%d. Available buf size %u < frame size %u\n",
4106  NI_MAX_ENCODER_QUERY_RETRIES, retval_backup,
4107  buf_info.buf_avail_size, frame_size_bytes);
4109 
4110  LRETURN;
4111  }
4112  send_count++;
4113  ni_pthread_mutex_unlock(&p_ctx->mutex);
4115  ni_pthread_mutex_lock(&p_ctx->mutex);
4116  } else
4117  {
4118  ni_log2(p_ctx, NI_LOG_DEBUG,
4119  "Info enc write query success, available buf "
4120  "size %u >= frame size %u !\n",
4121  buf_info.buf_avail_size, frame_size_bytes);
4122  break;
4123  }
4124  }
4125  }
4126 
4127  // fill in metadata such as timestamp
4128  ni_metadata_enc_frame_t *p_meta;
4129  if (separate_metadata)
4130  {
4131  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
4132  } else
4133  {
4134  p_meta = (ni_metadata_enc_frame_t *)((uint8_t *)
4135  p_frame->p_data[2 + ishwframe] +
4136  p_frame->data_len[2 + ishwframe]);
4137  }
4138 
4139  if (p_meta) //When hwframe xcoding reaches eos, frame looks like swframe but no allocation for p_meta
4140  {
4141  p_meta->metadata_common.ui64_data.frame_tstamp = (uint64_t)p_frame->pts;
4142 
4143  p_meta->force_headers = 0; // p_frame->force_headers not implemented/used
4146  p_meta->use_long_term_ref = p_frame->use_long_term_ref;
4147 
4148  ni_log2(p_ctx, NI_LOG_DEBUG,
4149  "%s: p_meta "
4150  "use_cur_src_as_long_term_pic %d use_long_term_ref %d\n",
4151  __func__, p_meta->use_cur_src_as_long_term_pic,
4152  p_meta->use_long_term_ref);
4153 
4154  p_meta->frame_force_type_enable = p_meta->frame_force_type = 0;
4155  // frame type to be forced to is supposed to be set correctly
4156  // in p_frame->ni_pict_type
4157  if (1 == p_ctx->force_frame_type || p_frame->force_key_frame)
4158  {
4159  if (p_frame->ni_pict_type)
4160  {
4161  p_meta->frame_force_type_enable = 1;
4162  p_meta->frame_force_type = p_frame->ni_pict_type;
4163  }
4164  ni_log2(p_ctx, NI_LOG_DEBUG,
4165  "%s(): ctx->force_frame_type"
4166  " %d frame->force_key_frame %d force frame_num %" PRIu64 ""
4167  " type to %d\n",
4168  __func__, p_ctx->force_frame_type, p_frame->force_key_frame,
4169  p_ctx->frame_num, p_frame->ni_pict_type);
4170  }
4171 
4172  // force pic qp if specified
4173  p_meta->force_pic_qp_enable = p_meta->force_pic_qp_i =
4174  p_meta->force_pic_qp_p = p_meta->force_pic_qp_b = 0;
4175  if (p_frame->force_pic_qp)
4176  {
4177  p_meta->force_pic_qp_enable = 1;
4178  p_meta->force_pic_qp_i = p_meta->force_pic_qp_p =
4179  p_meta->force_pic_qp_b = p_frame->force_pic_qp;
4180  }
4181  p_meta->frame_sei_data_size = p_frame->sei_total_len;
4182  p_meta->frame_roi_map_size = p_frame->roi_len;
4183  p_meta->frame_roi_avg_qp = p_ctx->roi_avg_qp;
4184  p_meta->enc_reconfig_data_size = p_frame->reconf_len;
4185 
4187  "6Q") >= 0)
4188  {
4189  if (separate_start)
4190  {
4191  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4192  p_meta->start_len[i] = p_frame->start_len[i];
4193  }
4194  else
4195  {
4196  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
4197  }
4199  }
4200 
4201  if (p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr == 0)
4202  p_meta->get_recon_frame_mode = ((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode;
4203  else
4204  p_meta->get_recon_frame_mode = 3;
4205 
4206  ni_log2(p_ctx,
4207  NI_LOG_DEBUG,
4208  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
4209  "p_frame->start_of_stream=%u, p_frame->end_of_stream=%u, "
4210  "p_frame->video_width=%u, p_frame->video_height=%u, pts=0x%08x 0x%08x, "
4211  "dts=0x%08x 0x%08x, sei_len=%u, roi size=%u avg_qp=%u reconf_len=%u "
4212  "force_pic_qp=%u use_cur_src_as_long_term_pic %u use_long_term_ref "
4213  "%u start_len [%u,%u,%u] inconsecutive_transfer %u\n",
4214  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
4215  p_frame->start_of_stream, p_frame->end_of_stream, p_frame->video_width,
4216  p_frame->video_height, (uint32_t)((p_frame->pts >> 32) & 0xFFFFFFFF),
4217  (uint32_t)(p_frame->pts & 0xFFFFFFFF),
4218  (uint32_t)((p_frame->dts >> 32) & 0xFFFFFFFF),
4219  (uint32_t)(p_frame->dts & 0xFFFFFFFF), p_meta->frame_sei_data_size,
4220  p_meta->frame_roi_map_size, p_meta->frame_roi_avg_qp,
4221  p_meta->enc_reconfig_data_size, p_meta->force_pic_qp_i,
4223  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
4224  p_meta->inconsecutive_transfer);
4225  }
4226  if (p_frame->start_of_stream)
4227  {
4228  //Send Start of stream p_config command here
4230  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4231  p_ctx->device_type, p_ctx->hw_id,
4232  &(p_ctx->session_id), OPT_1);
4233  CHECK_VPU_RECOVERY(retval);
4234  if (NI_RETCODE_SUCCESS != retval)
4235  {
4236  LRETURN;
4237  }
4238 
4239  p_frame->start_of_stream = 0;
4240  }
4241 
4242  // skip direct to send eos without sending the passed in p_frame as it's been sent already
4243  if (p_frame->end_of_stream)
4244  {
4246  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
4247  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4248  CHECK_VPU_RECOVERY(retval);
4249  if (NI_RETCODE_SUCCESS != retval)
4250  {
4251  LRETURN;
4252  }
4253 
4254  p_frame->end_of_stream = 0;
4255  p_ctx->ready_to_close = 1;
4256  } else //handle regular frame sending
4257  {
4258  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
4259  p_frame->dts, 0);
4260  if (NI_RETCODE_SUCCESS != retval)
4261  {
4262  ni_log2(p_ctx, NI_LOG_ERROR,
4263  "ERROR %s(): "
4264  "ni_timestamp_register() for dts returned: %d\n",
4265  __func__, retval);
4266  }
4267 
4268  if (separate_metadata)
4269  {
4270  uint32_t ui32LBA_metadata =
4272  ni_log2(p_ctx, NI_LOG_DEBUG,
4273  "%s: p_metadata_buffer = %p, metadata_buffer_size "
4274  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4275  __func__, p_frame->p_metadata_buffer,
4276  p_frame->metadata_buffer_size, p_ctx->frame_num,
4277  ui32LBA_metadata);
4278 
4279  sent_size =
4281 
4282  retval = ni_nvme_send_write_cmd(
4283  p_ctx->blk_io_handle, p_ctx->event_handle,
4284  p_frame->p_metadata_buffer, sent_size,
4285  ui32LBA_metadata);
4286  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4287  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4288  CHECK_VPU_RECOVERY(retval);
4289  if (retval < 0)
4290  {
4291  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
4292  __func__);
4294  LRETURN;
4295  }
4296  }
4297 
4298  if (separate_start)
4299  {
4300  uint32_t ui32LBA =
4302  ni_log2(p_ctx, NI_LOG_DEBUG,
4303  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
4304  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4305  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
4306  ui32LBA);
4307 
4308  sent_size =
4310 
4311  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4312  p_frame->p_start_buffer, sent_size, ui32LBA);
4313  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4314  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4315  CHECK_VPU_RECOVERY(retval);
4316  if (retval < 0)
4317  {
4318  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4320  LRETURN;
4321  }
4322  }
4323 
4324  if (p_frame->inconsecutive_transfer)
4325  {
4326  uint32_t ui32LBA =
4328 
4329  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
4330  {
4331  ni_log2(p_ctx, NI_LOG_DEBUG,
4332  "%s: p_data = %p, p_frame->buffer_size "
4333  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4334  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4335  ui32LBA);
4336 
4337  if (p_frame->data_len[i])
4338  {
4339  sent_size = p_frame->data_len[i];
4340  if (separate_start)
4341  sent_size -= p_frame->start_len[i];
4342 
4343  sent_size =
4345 
4346  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4347  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
4348  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4349  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4350  CHECK_VPU_RECOVERY(retval);
4351  if (retval < 0)
4352  {
4353  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4355  LRETURN;
4356  }
4357  }
4358  }
4359  }
4360  else
4361  {
4362  uint32_t ui32LBA =
4364  ni_log2(p_ctx, NI_LOG_DEBUG,
4365  "%s: p_data = %p, p_frame->buffer_size "
4366  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
4367  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
4368  ui32LBA);
4369 
4370  sent_size = frame_size_bytes;
4371  if (separate_metadata)
4372  sent_size -= p_frame->extra_data_len;
4373  if (separate_start)
4374  sent_size -= p_frame->total_start_len;
4375 
4376  sent_size =
4378 
4379  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4380  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
4381  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
4382  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4383  CHECK_VPU_RECOVERY(retval);
4384  if (retval < 0)
4385  {
4386  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4388  LRETURN;
4389  }
4390  }
4391 
4392  //Save input frame data used for calculate PSNR
4393  if ((ni_cmp_fw_api_ver((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rc") >= 0) &&
4394  (p_ctx->frame_num == 0 || ((p_ctx->frame_num % ((ni_xcoder_params_t *)(p_ctx->p_session_config))->interval_of_psnr) == 0)) &&
4395  (((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode != 3) &&
4396  (!(((ni_xcoder_params_t *)(p_ctx->p_session_config))->cfg_enc_params.get_psnr_mode == 2 && p_ctx->codec_format == NI_CODEC_FORMAT_H265)))
4397  {
4398  if (ishwframe && !p_frame->end_of_stream)
4399  {
4400  // uint16_t input_frame_idx = ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx;
4401  // use hwdownload to download hw frame, but donot recycle hwframe buffer
4402  ni_session_data_io_t hwdl_session_data = {0};
4403  // ni_log2(p_ctx, NI_LOG_ERROR, "pixel_format %d\n", p_ctx->pixel_format);
4404  int ret = hwdl_frame(p_ctx, &hwdl_session_data, p_frame,
4405  p_frame->pixel_format);
4406  if (ret <= 0)
4407  {
4408  ni_frame_buffer_free(&(hwdl_session_data.data.frame));
4409  return ret;
4410  }
4411 
4412  hwdl_session_data.data.frame.pts = p_frame->pts;
4413  enqueue_ni_frame(p_ctx, &hwdl_session_data.data.frame);
4414  ni_frame_buffer_free(&hwdl_session_data.data.frame);
4415  }
4416  else
4417  {
4418  enqueue_ni_frame(p_ctx, p_frame);
4419  }
4420  }
4421 
4422  if (ishwframe)
4423  {
4424  ni_log2(p_ctx, NI_LOG_DEBUG,
4425  "%s(): session=0x%x ui16FrameIdx=%u\n",
4426  __func__,
4427  p_ctx->session_id,
4428  ((niFrameSurface1_t*)(p_frame->p_data[3]))->ui16FrameIdx);
4429  }
4430 
4431  p_ctx->status = 0;
4432  p_ctx->frame_num++;
4433  size = frame_size_bytes;
4434  p_ctx->low_delay_sync_flag = 1;
4435 
4436 #ifdef XCODER_DUMP_DATA
4437  char dump_file[256];
4438  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-fme/fme-%04ld.yuv",
4439  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
4440 
4441  FILE *f = fopen(dump_file, "wb");
4442  fwrite(p_frame->p_buffer,
4443  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
4444  1, f);
4445  fflush(f);
4446  fclose(f);
4447 #endif
4448  }
4449 
4450  retval = size;
4451 
4452 END:
4453 
4454  ni_pthread_mutex_unlock(&p_ctx->mutex);
4455 
4456  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4457  return retval;
4458 }
4459 
4460 
4462 {
4463  ni_instance_mgr_stream_info_t data = { 0 };
4464  uint32_t actual_read_size = 0;
4465  uint32_t to_read_size = 0;
4466  int size = 0;
4467  static long long encq_count = 0LL;
4468  int retval = NI_RETCODE_SUCCESS;
4469  int query_retry = 0;
4470  int query_type = INST_BUF_INFO_RW_READ;
4471  uint32_t ui32LBA = 0;
4472  ni_metadata_enc_bstream_t *p_meta = NULL;
4473  ni_metadata_enc_bstream_rev61_t *p_meta_rev61 = NULL;
4474  ni_instance_buf_info_t buf_info = { 0 };
4475  ni_session_statistic_t sessionStatistic = {0};
4476  int low_delay_notify = 0;
4477  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4478 
4479  if (!p_ctx || !p_packet || !p_packet->p_data)
4480  {
4481  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
4482  __func__);
4483  retval = NI_RETCODE_INVALID_PARAM;
4484  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4485 
4486  return retval;
4487  }
4488 
4489  ni_pthread_mutex_lock(&p_ctx->mutex);
4490 
4491  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
4492  {
4493  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
4494  __func__);
4496  LRETURN;
4497  }
4498 
4499  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 ", av1_pkt_num=%" PRIu64 "\n",
4500  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->av1_pkt_num);
4501  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode)
4502  {
4503  query_type = INST_BUF_INFO_RW_READ_BUSY;
4504  if (!p_packet->end_of_stream && p_ctx->frame_num < p_ctx->pkt_num)
4505  {
4506  if (p_ctx->ready_to_close)
4507  {
4508  query_type = INST_BUF_INFO_RW_READ;
4509  } else
4510  { //nothing to query, leave
4511  retval = NI_RETCODE_SUCCESS;
4512  low_delay_notify = 1;
4513  LRETURN;
4514  }
4515  }
4516  }
4517  for (;;)
4518  {
4519  query_sleep(p_ctx);
4520 
4521  query_retry++;
4522 
4524  "65") >= 0)
4525  {
4527  p_ctx, NI_DEVICE_TYPE_ENCODER, &sessionStatistic);
4528  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
4530  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
4531  CHECK_VPU_RECOVERY(retval);
4532 
4533  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
4534 
4535  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4536  {
4538  "6rX") >= 0)
4539  {
4540  if (p_ctx->current_frame_delay < (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay)
4541  {
4542  p_ctx->current_frame_delay = (int)sessionStatistic.ui8AdditionalFramesDelay + p_ctx->initial_frame_delay; // extend frames delay by FW estimated additional number of frames
4543  }
4544  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: initial_frame_delay %d max_frame_delay %d ui8AdditionalFramesDelay %u current_frame_delay %d\n",
4545  __FUNCTION__, p_ctx->initial_frame_delay, p_ctx->max_frame_delay, sessionStatistic.ui8AdditionalFramesDelay, p_ctx->current_frame_delay);
4546  }
4547  }
4548  } else
4549  {
4550  retval = ni_query_instance_buf_info(
4551  p_ctx, query_type, NI_DEVICE_TYPE_ENCODER, &buf_info);
4552  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4553  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4554  CHECK_VPU_RECOVERY(retval);
4555  }
4556 
4557  ni_log2(p_ctx, NI_LOG_TRACE,
4558  "Info enc read query rc %d, available buf size %u, "
4559  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
4560  retval, buf_info.buf_avail_size, p_ctx->frame_num, p_ctx->pkt_num);
4561 
4562  if (NI_RETCODE_SUCCESS != retval)
4563  {
4564  ni_log2(p_ctx, NI_LOG_ERROR, "Buffer info query failed in encoder read!!!!\n");
4565  LRETURN;
4566  } else if (0 == buf_info.buf_avail_size)
4567  {
4568  // query to see if it is eos now, if we have sent it
4569  if (p_ctx->ready_to_close)
4570  {
4571  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_ENCODER, &data);
4572  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
4573  p_ctx->device_type, p_ctx->hw_id,
4574  &(p_ctx->session_id), OPT_1);
4575  CHECK_VPU_RECOVERY(retval);
4576 
4577  if (NI_RETCODE_SUCCESS != retval)
4578  {
4579  ni_log2(p_ctx, NI_LOG_ERROR, "Stream info query failed in encoder read!!!!\n");
4580  LRETURN;
4581  }
4582 
4583  if (data.is_flushed)
4584  {
4585  p_packet->end_of_stream = 1;
4586  }
4587  }
4588  ni_log2(p_ctx, NI_LOG_DEBUG, "Info enc read available buf size %u, eos %u !\n",
4589  buf_info.buf_avail_size, p_packet->end_of_stream);
4590 
4591  if ((((ni_xcoder_params_t *)p_ctx->p_session_config)->low_delay_mode ||
4593  !p_packet->end_of_stream && p_ctx->frame_num >= p_ctx->pkt_num)
4594  {
4595  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder low latency mode, eos not sent, frame_num "
4596  "%" PRIu64 " >= %" PRIu64 " pkt_num, keep querying p_ctx->status %d\n",
4597  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->status);
4598  ni_pthread_mutex_unlock(&p_ctx->mutex);
4600  ni_pthread_mutex_lock(&p_ctx->mutex);
4601  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES &&
4603  {
4604  low_delay_notify = 1;
4606  LRETURN;
4607  }
4608  else
4609  {
4610  continue;
4611  }
4612  }
4613  else if (((ni_xcoder_params_t *)p_ctx->p_session_config)->minFramesDelay)
4614  {
4615  if (p_ctx->pkt_num) // do not busy read until header is received
4616  {
4617  uint64_t pkt_num = (p_ctx->codec_format == NI_CODEC_FORMAT_AV1) ? p_ctx->av1_pkt_num : p_ctx->pkt_num - 1; // deduct header from pkt_num
4618  if (p_ctx->frame_num - pkt_num >= p_ctx->current_frame_delay &&
4619  !p_packet->end_of_stream)
4620  {
4621  ni_log2(p_ctx, NI_LOG_TRACE, "%s: low delay mode 2, keep reading send frame %d receive pkt %d gop %d current_frame_delay %d\n",
4622  __FUNCTION__, p_ctx->frame_num, pkt_num,
4623  p_ctx->last_gop_size, p_ctx->current_frame_delay);
4624  continue;
4625  }
4626  }
4627  }
4628  retval = NI_RETCODE_SUCCESS;
4629  LRETURN;
4630  } else
4631  {
4632  break;
4633  }
4634  }
4635  ni_log2(p_ctx, NI_LOG_DEBUG, "Encoder read buf_avail_size %u\n", buf_info.buf_avail_size);
4636 
4637  to_read_size = buf_info.buf_avail_size;
4638 
4639  p_packet->data_len = 0;
4640  p_packet->pts = NI_NOPTS_VALUE;
4641  p_packet->dts = 0;
4642 
4644  actual_read_size = to_read_size;
4645  if (actual_read_size % NI_MEM_PAGE_ALIGNMENT)
4646  {
4647  actual_read_size =
4648  ((actual_read_size / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) +
4650  }
4651 
4652  if (p_packet->buffer_size < actual_read_size)
4653  {
4654  if (ni_packet_buffer_alloc(p_packet, actual_read_size))
4655  {
4656  ni_log2(p_ctx, NI_LOG_ERROR,
4657  "ERROR %s(): packet buffer size %u allocation "
4658  "failed\n",
4659  __func__, actual_read_size);
4660  retval = NI_RETCODE_ERROR_MEM_ALOC;
4661  LRETURN;
4662  }
4663  }
4664 
4665  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
4666  p_packet->p_data, actual_read_size, ui32LBA);
4667  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
4668  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
4669  CHECK_VPU_RECOVERY(retval);
4670  if (retval < 0)
4671  {
4672  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
4674  LRETURN;
4675  }
4676 
4677  // SSIM is supported if fw_rev is >= 6.2
4679  "62") >= 0)
4680  {
4681  p_meta = (ni_metadata_enc_bstream_t *)p_packet->p_data;
4682  p_packet->pts = (int64_t)(p_meta->frame_tstamp);
4683  p_packet->frame_type = p_meta->frame_type;
4684  p_packet->avg_frame_qp = p_meta->avg_frame_qp;
4685  p_packet->recycle_index = p_meta->recycle_index;
4686  p_packet->av1_show_frame = p_meta->av1_show_frame;
4687  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta->recycle_index);
4688  ni_log2(p_ctx, NI_LOG_DEBUG, "%s MetaDataSize %d FrameType %d AvgFrameQp %d ssim %d %d %d\n",
4689  __FUNCTION__, p_meta->metadata_size, p_meta->frame_type, p_meta->avg_frame_qp, p_meta->ssimY, p_meta->ssimU, p_meta->ssimV);
4690 
4692  "6r2") >= 0)
4693  {
4694  if (((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth)
4695  {
4697  "6rX") < 0)
4698  {
4699  if (p_meta->gop_size) // ignore frame 0 gop size 0 (other I-frame gop size 1)
4700  {
4701  if ((int)p_meta->gop_size < p_ctx->last_gop_size)
4702  {
4703  p_ctx->current_frame_delay = p_ctx->max_frame_delay; // shortening gop (including I-frame) causes lookahead queue increase, currently assume worst case frame delay
4704  }
4705  }
4706  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: current gop_size %u last_gop_size %d initial_frame_delay %d max_frame_delay %d current_frame_delay %d\n",
4707  __FUNCTION__, p_meta->gop_size, p_ctx->last_gop_size, p_ctx->initial_frame_delay, p_ctx->max_frame_delay, p_ctx->current_frame_delay);
4708  }
4709  p_ctx->last_gop_size = p_meta->gop_size;
4710  }
4711  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: current gop_size %u\n",
4712  __FUNCTION__, p_meta->gop_size);
4713  }
4714 
4716  "6p") >= 0)
4717  {
4718  ni_log2(p_ctx, NI_LOG_DEBUG, "max_mv x[0] %d x[1] %d y[0] %d y[1] %d min_mv x[0] %d x[1] %d y[0] %d y[1] %d frame_size %u inter_total_count %u intra_total_count %u\n",
4719  __FUNCTION__,
4720  p_meta->max_mv_x[0], p_meta->max_mv_x[1], p_meta->max_mv_y[0], p_meta->max_mv_y[1],
4721  p_meta->min_mv_x[0], p_meta->min_mv_x[1], p_meta->min_mv_y[0], p_meta->min_mv_y[1],
4722  p_meta->frame_size, p_meta->inter_total_count, p_meta->intra_total_count);
4723  }
4724 
4725  p_ctx->meta_size = p_meta->metadata_size;
4726 
4727  if (p_meta->ssimY != 0)
4728  {
4729  // The SSIM Y, U, V values returned by FW are 4 decimal places multiplied by 10000.
4730  //Divide by 10000 to get the original value.
4731  ni_log2(p_ctx,
4732 #ifdef NI_LOG_SSIM_AT_INFO
4733  NI_LOG_INFO,
4734 #else
4735  NI_LOG_DEBUG,
4736 #endif
4737  "%s: pkt #%" PRId64 " pts %" PRId64 " ssim "
4738  "Y %.4f U %.4f V %.4f\n", __FUNCTION__, p_ctx->pkt_num,
4739  p_packet->pts, (float)p_meta->ssimY/10000,
4740  (float)p_meta->ssimU/10000, (float)p_meta->ssimV/10000);
4741  }
4742  }
4743  else
4744  {
4745  // Up to fw_rev major 6 and minor 1, use the old meta data structure
4746  p_meta_rev61 = (ni_metadata_enc_bstream_rev61_t *)p_packet->p_data;
4747  p_packet->pts = (int64_t)(p_meta_rev61->frame_tstamp);
4748  p_packet->frame_type = p_meta_rev61->frame_type;
4749  p_packet->avg_frame_qp = p_meta_rev61->avg_frame_qp;
4750  p_packet->recycle_index = p_meta_rev61->recycle_index;
4751  p_packet->av1_show_frame = p_meta_rev61->av1_show_frame;
4752  ni_log2(p_ctx, NI_LOG_DEBUG, "%s RECYCLE INDEX = %u!!!\n", __FUNCTION__, p_meta_rev61->recycle_index);
4753 
4754  }
4755 
4756  p_packet->data_len = to_read_size;
4757 
4758  size = p_packet->data_len;
4759 
4760  if (size > 0)
4761  {
4763  "6rc") >= 0)
4764  {
4765  calculate_psnr(p_ctx, p_packet);
4766  }
4767 
4768  if (p_ctx->pkt_num >= 1)
4769  {
4770  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
4771  || p_packet->av1_show_frame)
4772  {
4773  if (ni_timestamp_get_with_threshold(p_ctx->dts_queue, 0, (int64_t*)& p_packet->dts, 0, encq_count % 500 == 0, p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
4774  {
4775  p_packet->dts = NI_NOPTS_VALUE;
4776  }
4777  }
4778 
4779  p_ctx->pkt_num++;
4780  if (p_ctx->codec_format == NI_CODEC_FORMAT_AV1 && p_packet->recycle_index != (uint32_t) NI_AV1_INVALID_BUFFER_INDEX) // av1 invalid buffer index indicates show_existing_frame header packet
4781  {
4782  p_ctx->av1_pkt_num++;
4783  }
4784  low_delay_notify = 1;
4785  }
4786 
4787  encq_count++;
4788 
4789 #ifdef MEASURE_LATENCY
4790 #ifndef XCODER_311
4791  ni_log2(p_ctx, NI_LOG_INFO, "ENC fme_num %d, pkt_num %d, latency is %d\n",
4792  p_ctx->frame_num, p_ctx->pkt_num, p_ctx->frame_num - p_ctx->pkt_num);
4793 #endif
4794 #endif
4795 
4796 #ifdef XCODER_DUMP_DATA
4797  char dump_file[256];
4798  snprintf(dump_file, sizeof(dump_file), "%ld-%u-enc-pkt/pkt-%04ld.bin",
4799  (long)getpid(), p_ctx->session_id, (long)p_ctx->pkt_num);
4800 
4801  FILE *f = fopen(dump_file, "wb");
4802  fwrite((uint8_t *)p_packet->p_data + sizeof(ni_metadata_enc_bstream_t),
4803  p_packet->data_len - sizeof(ni_metadata_enc_bstream_t), 1, f);
4804  fflush(f);
4805  fclose(f);
4806 #endif
4807  }
4808 
4809  ni_log2(p_ctx,
4810  NI_LOG_DEBUG,
4811  "%s(): %d.%u p_packet->start_of_stream=%u, "
4812  "p_packet->end_of_stream=%u, p_packet->video_width=%u, "
4813  "p_packet->video_height=%u, p_packet->dts=0x%08x 0x%08x, "
4814  "p_packet->pts=0x%08x 0x%08x, type=%u, avg_frame_qp=%u, show_frame=%d\n",
4815  __func__, p_ctx->hw_id, p_ctx->session_id, p_packet->start_of_stream,
4816  p_packet->end_of_stream, p_packet->video_width, p_packet->video_height,
4817  (uint32_t)((p_packet->dts >> 32) & 0xFFFFFFFF),
4818  (uint32_t)(p_packet->dts & 0xFFFFFFFF),
4819  (uint32_t)((p_packet->pts >> 32) & 0xFFFFFFFF),
4820  (uint32_t)(p_packet->pts & 0xFFFFFFFF), p_packet->frame_type,
4821  p_packet->avg_frame_qp, p_packet->av1_show_frame);
4822 
4823  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_packet->data_len=%u, size=%d\n", __func__,
4824  p_packet->data_len, size);
4825 
4826  if (encq_count % 500 == 0)
4827  {
4828  ni_log2(p_ctx, NI_LOG_DEBUG,
4829  "Encoder pts queue size = %u dts queue size = %u\n\n",
4830  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
4831  }
4832 
4833  retval = size;
4834 
4835 #ifdef MEASURE_LATENCY
4836  if ((p_packet->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL) && (p_ctx->pkt_num > 0))
4837  {
4838  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format
4839  || p_packet->av1_show_frame)
4840  {
4841  uint64_t abs_time_ns = ni_gettime_ns();
4843  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",eLAT:%" PRIu64 ";\n",
4844  p_packet->dts, abs_time_ns - q->last_benchmark_time,
4845  ni_lat_meas_q_check_latency(q, abs_time_ns, p_packet->dts));
4846  q->last_benchmark_time = abs_time_ns;
4847  }
4848  }
4849 #endif
4850 
4851 END:
4852 
4853  ni_pthread_mutex_unlock(&p_ctx->mutex);
4854 
4855  if (low_delay_notify)
4856  {
4857  low_delay_signal(p_ctx);
4858  }
4859 
4860  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4861 
4862  return retval;
4863 }
4864 
4865 /*!******************************************************************************
4866  * \brief Send sequnce change to a xcoder encoder instance
4867  *
4868  * \param
4869  *
4870  * \return
4871  *******************************************************************************/
4873 {
4875 
4876  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4877 
4878  // re-init last_gop_size and av1_pkt_num for minFramesDelay frame delay estimiation
4879  int lookAheadEnable = !!(((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.lookAheadDepth);
4880  int gop_preset_index = ((ni_xcoder_params_t *)p_ctx->p_session_config)->cfg_enc_params.gop_preset_index;
4881  p_ctx->last_gop_size = g_map_preset_to_gopsize[lookAheadEnable][gop_preset_index + 1];
4882  p_ctx->av1_pkt_num = 0;
4883 
4884  //Configure encoder sequence change
4886  p_resolution);
4888  p_ctx->device_type, p_ctx->hw_id,
4889  &(p_ctx->session_id), OPT_1);
4890  CHECK_VPU_RECOVERY(retval);
4891  if (retval < 0)
4892  {
4893  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): config encoder sequence change command failed\n",
4894  __func__);
4896  }
4897 
4898 END:
4899 
4900  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
4901 
4902  return retval;
4903 }
4904 
4905 /*!******************************************************************************
4906  * \brief Open a xcoder scaler instance
4907  *
4908  * \param[in] p_ctx pointer to session context
4909  *
4910  * \return NI_RETCODE_INVALID_PARAM
4911  * NI_RETCODE_ERROR_NVME_CMD_FAILED
4912  * NI_RETCODE_ERROR_INVALID_SESSION
4913  * NI_RETCODE_ERROR_MEM_ALOC
4914  *******************************************************************************/
4916 {
4918  void* p_buffer = NULL;
4919  uint32_t ui32LBA = 0;
4920  char fmt_fw_api_ver1[5], fmt_fw_api_ver2[5];
4921 
4922  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
4923 
4924  if (!p_ctx)
4925  {
4926  retval = NI_RETCODE_INVALID_PARAM;
4927  LRETURN;
4928  }
4929 
4931  {
4933  "64") < 0)
4934  {
4935  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use stack filter on device with FW API version < 6.4\n");
4937  }
4938  }
4939 
4941  {
4943  "67") < 0)
4944  {
4945  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use rotate filter on device with FW API version < 6.7\n");
4947  }
4948  }
4949 
4951  {
4953  "6L") < 0)
4954  {
4955  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot use in-place overlay filter on device with FW API version < 6.L\n");
4957  }
4958  }
4959 
4960  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
4961  {
4963  p_ctx->pts_table = NULL;
4964  p_ctx->dts_queue = NULL;
4965  p_ctx->p_leftover = NULL;
4966  p_ctx->buffer_pool = NULL;
4967  p_ctx->dec_fme_buf_pool = NULL;
4968  p_ctx->prev_size = 0;
4969  p_ctx->sent_size = 0;
4970  p_ctx->status = 0;
4971  p_ctx->key_frame_type = 0;
4972  p_ctx->ready_to_close = 0;
4973  p_ctx->rc_error_count = 0;
4974  p_ctx->frame_num = 0;
4975  p_ctx->pkt_num = 0;
4976  p_ctx->pkt_index = 0;
4977 
4978  //malloc zero data buffer
4979  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
4981  {
4982  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
4983  NI_ERRNO, __func__);
4984  retval = NI_RETCODE_ERROR_MEM_ALOC;
4985  LRETURN;
4986  }
4987  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
4988 
4989  //malloc data buffer
4990  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
4991  {
4992  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
4993  NI_ERRNO, __func__);
4994  retval = NI_RETCODE_ERROR_MEM_ALOC;
4995  LRETURN;
4996  }
4997  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
4998 
4999  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
5000  //In case we can open sesison, the session id would become valid.
5001  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
5002  (uint16_t)NI_INVALID_SESSION_ID;
5003 
5004  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
5005  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_SCALER, ni_htonl(p_ctx->scaler_operation), 0);
5006  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5007  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5008  if (retval != NI_RETCODE_SUCCESS)
5009  {
5010  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
5011  LRETURN;
5012  }
5013  //Open will return a session status structure with a valid session id if it worked.
5014  //Otherwise the invalid session id set before the open command will stay
5015  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
5016  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
5017  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
5018  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
5019  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5020  {
5021  ni_log2(p_ctx, NI_LOG_ERROR,
5022  "ERROR %s(): p_ctx->device_handle=%" PRIx64
5023  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5024  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5025  p_ctx->session_id);
5027  LRETURN;
5028  }
5029  ni_log2(p_ctx, NI_LOG_DEBUG, "Scaler open session ID:0x%x\n",p_ctx->session_id);
5030  ni_log2(p_ctx, NI_LOG_DEBUG,
5031  "%s(): p_ctx->device_handle=%" PRIx64
5032  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
5033  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
5034  p_ctx->session_id);
5035 
5036  //Send keep alive timeout Info
5037  uint64_t keep_alive_timeout =
5038  p_ctx->keep_alive_timeout * 1000000; //send us to FW
5039  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5040  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
5041  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
5042  keep_alive_timeout);
5044  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5045  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5046  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5047  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5048  CHECK_VPU_RECOVERY(retval);
5049 
5050  if (NI_RETCODE_SUCCESS != retval)
5051  {
5052  ni_log2(p_ctx, NI_LOG_ERROR,
5053  "ERROR %s(): nvme write keep_alive_timeout command "
5054  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5055  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5057  LRETURN;
5058  }
5059 
5060  // Send SW version to FW if FW API version is >= 6.2
5062  "62") >= 0)
5063  {
5064  // Send SW version to session manager
5065  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5066  memcpy(p_buffer, NI_XCODER_REVISION, sizeof(uint64_t));
5068  ni_fmt_fw_api_ver_str((char*) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], &fmt_fw_api_ver2[0]);
5069  ni_log2(p_ctx, NI_LOG_DEBUG, "%s libxcoder FW API ver %s, FW FW API ver %s\n",
5070  __func__, fmt_fw_api_ver1, fmt_fw_api_ver2);
5071  ui32LBA = CONFIG_SESSION_SWVersion_W(p_ctx->session_id);
5072  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5073  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
5074  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
5075  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5076  CHECK_VPU_RECOVERY(retval);
5077 
5078  if (NI_RETCODE_SUCCESS != retval)
5079  {
5080  ni_log2(p_ctx, NI_LOG_ERROR,
5081  "ERROR %s(): nvme write sw_version command "
5082  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
5083  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
5085  LRETURN;
5086  }
5087  }
5088  }
5089 
5090  // init for frame pts calculation
5091  p_ctx->is_first_frame = 1;
5092  p_ctx->last_pts = 0;
5093  p_ctx->last_dts = 0;
5094  p_ctx->active_video_width = 0;
5095  p_ctx->active_video_height = 0;
5096  p_ctx->actual_video_width = 0;
5097 
5098 #ifndef _WIN32
5099  if (p_ctx->isP2P)
5100  {
5101  retval = p2p_fill_pcie_address(p_ctx);
5102  if(retval != NI_RETCODE_SUCCESS)
5103  {
5104  LRETURN;
5105  }
5106  }
5107 #endif
5108 
5109 END:
5110 
5111  ni_aligned_free(p_buffer);
5112  return retval;
5113 }
5114 
5115 /*!******************************************************************************
5116  * \brief close a scaler session
5117  *
5118  * \param[in] p_ctx pointer to session context
5119  * \param[in] eos_received (not used)
5120  *
5121  * \return NI_RETCODE_INVALID_PARAM
5122  * NI_RETCODE_ERROR_INVALID_SESSION
5123  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5124  *******************************************************************************/
5126 {
5128  void * p_buffer = NULL;
5129  uint32_t ui32LBA = 0;
5130 
5131  if (!p_ctx)
5132  {
5133  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5134  __func__);
5135  return NI_RETCODE_INVALID_PARAM;
5136  }
5137 
5138  ni_pthread_mutex_lock(&p_ctx->mutex);
5139 
5140  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5141  {
5142  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
5143  retval = NI_RETCODE_SUCCESS;
5144  LRETURN;
5145  }
5146 
5147  //malloc data buffer
5148  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
5149  {
5150  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc data buffer failed\n",
5151  NI_ERRNO, __func__);
5152  retval = NI_RETCODE_ERROR_MEM_ALOC;
5153  LRETURN;
5154  }
5155  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
5156 
5158 
5159  int retry = 0;
5160  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
5161  {
5162  ni_log2(p_ctx, NI_LOG_DEBUG,
5163  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
5164  "p_ctx->session_id=%d, close_mode=1\n",
5165  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5166  p_ctx->session_id);
5167 
5169  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
5170  {
5171  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed!\n", __func__);
5174  break;
5175  } else
5176  {
5177  //Close should always succeed
5178  retval = NI_RETCODE_SUCCESS;
5180  break;
5181  }
5182  retry++;
5183  }
5184 
5185 #ifndef _WIN32
5186  if (p_ctx->isP2P)
5187  {
5188  if (p_ctx->netint_fd)
5189  {
5190  close(p_ctx->netint_fd);
5191  }
5192  }
5193 #endif
5194 
5195 END:
5196 
5197  ni_aligned_free(p_buffer);
5199 
5200  ni_pthread_mutex_unlock(&p_ctx->mutex);
5201 
5202  return retval;
5203 }
5204 
5205 /*!******************************************************************************
5206  * \brief Send a p_config command to configure scaling parameters.
5207  *
5208  * \param ni_session_context_t p_ctx - xcoder Context
5209  * \param ni_scaler_params_t * params - pointer to the scaler ni_scaler_params_t struct
5210  *
5211  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5212  *******************************************************************************/
5214  ni_scaler_params_t *p_params)
5215 {
5216  void *p_scaler_config = NULL;
5217  ni_scaler_config_t *p_cfg = NULL;
5218  uint32_t buffer_size = sizeof(ni_scaler_params_t);
5220  uint32_t ui32LBA = 0;
5221 
5222  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5223 
5224  if (!p_ctx || !p_params)
5225  {
5226  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5227  __func__);
5228  retval = NI_RETCODE_INVALID_PARAM;
5229  LRETURN;
5230  }
5231 
5232  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5233  {
5234  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5235  __func__);
5237  LRETURN;
5238  }
5239 
5240  buffer_size =
5241  ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) *
5243  if (ni_posix_memalign(&p_scaler_config, sysconf(_SC_PAGESIZE), buffer_size))
5244  {
5245  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() malloc p_scaler_config buffer failed\n",
5246  NI_ERRNO, __func__);
5247  retval = NI_RETCODE_ERROR_MEM_ALOC;
5248  LRETURN;
5249  }
5250  memset(p_scaler_config, 0, buffer_size);
5251 
5252  //configure the session here
5255 
5256  //Flip the bytes!!
5257  p_cfg = (ni_scaler_config_t *)p_scaler_config;
5258  p_cfg->filterblit = p_params->filterblit;
5259  p_cfg->numInputs = p_params->nb_inputs;
5260 
5261  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5262  p_scaler_config, buffer_size, ui32LBA);
5263  if ((int32_t)retval < 0)
5264  {
5265  ni_log2(p_ctx, NI_LOG_ERROR,
5266  "ERROR: ni_nvme_send_write_cmd failed: blk_io_handle: %" PRIx64
5267  ", hw_id, %d, xcoder_inst_id: %d\n",
5268  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5269  // Close the session since we can't configure it as per fw
5270  retval = ni_scaler_session_close(p_ctx, 0);
5271  if (NI_RETCODE_SUCCESS != retval)
5272  {
5273  ni_log2(p_ctx, NI_LOG_ERROR,
5274  "ERROR: %s failed: blk_io_handle: %" PRIx64 ","
5275  "hw_id, %d, xcoder_inst_id: %d\n",
5276  __func__,
5277  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
5278  p_ctx->session_id);
5279  }
5280 
5282  LRETURN;
5283  }
5284  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5285  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5286 
5287 END:
5288 
5289  ni_aligned_free(p_scaler_config);
5290  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5291 
5292  return retval;
5293 }
5294 
5295 /*!******************************************************************************
5296  * \brief allocate a frame in the scaler
5297  *
5298  * \param[in] p_ctx pointer to session context
5299  * \param[in] width width in pixels
5300  * \param[in] height height in pixels
5301  * \param[in] format pixel format
5302  * \param[in] options option flags
5303  * \param[in] rectangle_width clipping rectangle width in pixels
5304  * \param[in] rectangle_height clipping rectangle height in pixels
5305  * \param[in] rectangle_x clipping rectangle x position
5306  * \param[in] rectangle_y clipping rectangle y position
5307  * \param[in] rgba_color background colour (only used by pad filter)
5308  * \param[in] frame_index frame index (only for hardware frames)
5309  *
5310  * \return NI_RETCODE_INVALID_PARAM
5311  * NI_RETCODE_ERROR_INVALID_SESSION
5312  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5313  * NI_RETCODE_ERROR_MEM_ALOC
5314  *******************************************************************************/
5316  int width,
5317  int height,
5318  int format,
5319  int options,
5320  int rectangle_width,
5321  int rectangle_height,
5322  int rectangle_x,
5323  int rectangle_y,
5324  int rgba_color,
5325  int frame_index)
5326 {
5329  uint32_t dataLen;
5330  uint32_t ui32LBA = 0;
5331  uint32_t query_retry = 0;
5332 
5333  /* Round up to nearest 4096 bytes */
5334  dataLen =
5336  dataLen = dataLen & 0xFFFFF000;
5337 
5338  if (!p_ctx)
5339  {
5340  return NI_RETCODE_INVALID_PARAM;
5341  }
5342 
5343  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5344  {
5345  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5346  __func__);
5348  }
5349 
5350  if (((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)))
5351  {
5352  // this operation is to free/allocate scaler frame pool
5353  if (rgba_color == 0)
5354  {
5355  if (ni_cmp_fw_api_ver(
5357  "6r3") < 0)
5358  {
5359  ni_log2(p_ctx, NI_LOG_INFO,
5360  "WARNING: Allocate framepool size 0 for session 0x%x\n", p_ctx->session_id);
5361  return NI_RETCODE_SUCCESS;
5362  }
5363  else if (p_ctx->pool_type == NI_POOL_TYPE_NONE)
5364  {
5365  ni_log2(p_ctx, NI_LOG_ERROR,
5366  "ERROR: %s() try to free session 0x%x framepool while it's not allocated\n",
5367  __func__, p_ctx->session_id);
5368  return NI_RETCODE_INVALID_PARAM;
5369  }
5370  else if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5371  {
5372  ni_log2(p_ctx, NI_LOG_INFO, "Free framepool of scaler 0x%x\n", p_ctx->session_id);
5373  }
5374  else
5375  {
5376  ni_log2(p_ctx, NI_LOG_ERROR,
5377  "ERROR: %s() try to free session 0x%x framepool of type %u while "
5378  "passing type %u\n",
5379  __func__, p_ctx->session_id, p_ctx->pool_type,
5380  (options & NI_SCALER_FLAG_P2));
5381  }
5382  }
5383  else
5384  {
5385  if (p_ctx->pool_type != NI_POOL_TYPE_NONE)
5386  {
5387  // try to expand the framepool
5388  if (ni_cmp_fw_api_ver(
5390  "6r3") < 0)
5391  {
5392  ni_log2(p_ctx, NI_LOG_ERROR,
5393  "ERROR: allocate framepool multiple times for session 0x%x "
5394  "not supported in FW API version < 6r3\n",
5395  p_ctx->session_id);
5396  return NI_RETCODE_INVALID_PARAM;
5397  }
5398  if ((options & NI_SCALER_FLAG_P2) == p_ctx->pool_type)
5399  {
5400  ni_log2(p_ctx, NI_LOG_INFO,
5401  "Expand frame pool of scaler 0x%x with %u more frames\n",
5402  p_ctx->session_id, rgba_color);
5403  }
5404  else
5405  {
5406  ni_log2(p_ctx, NI_LOG_ERROR,
5407  "ERROR: try to expand session 0x%x framepool with type %u "
5408  "while pool type is %u\n",
5409  p_ctx->session_id, options & NI_SCALER_FLAG_P2, p_ctx->pool_type);
5410  return NI_RETCODE_INVALID_PARAM;
5411  }
5412  }
5413  }
5414  }
5415 
5416  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5417  {
5418  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5419  NI_ERRNO, __func__);
5421  }
5422 
5423  memset(p_data, 0x00, dataLen);
5424 
5425  p_data->picture_width = width;
5426  p_data->picture_height = height;
5427  p_data->picture_format = format;
5428  p_data->options = options;
5429  p_data->rectangle_width = rectangle_width;
5430  p_data->rectangle_height = rectangle_height;
5431  p_data->rectangle_x = rectangle_x;
5432  p_data->rectangle_y = rectangle_y;
5433  p_data->rgba_color = rgba_color;
5434  p_data->frame_index = frame_index;
5435  bool isrgb = ((GC620_RGBA8888 == format) || (GC620_BGRX8888 == format) ||
5436  (GC620_ARGB8888 == format) || (GC620_ABGR8888 == format));
5437  if(width > NI_MAX_RESOLUTION_WIDTH || height > NI_MAX_RESOLUTION_HEIGHT ||
5439  ((width > NI_MAX_RESOLUTION_RGBA_WIDTH || height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5440  {
5441  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", width, height, format);
5442  ni_aligned_free(p_data);
5444  }
5445  ni_log2(p_ctx, NI_LOG_DEBUG,
5446  "Session=0x%x: Dev alloc frame: FrameIndex=%d; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5447  p_ctx->session_id, frame_index,
5448  p_data->picture_width, p_data->picture_height,
5449  p_data->picture_format, p_data->rectangle_width,
5450  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5451 
5454 
5455  for (;;)
5456  {
5457  query_retry++;
5458 
5459  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5460  p_data, dataLen, ui32LBA);
5461 
5462  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5463  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_3);
5464 
5466  {
5467  if (query_retry >= 1000 || (options & NI_SCALER_FLAG_PC))
5468  {
5469  ni_log2(p_ctx, NI_LOG_ERROR, "Warning: 2D could not acquire frame\n");
5470  retval = NI_RETCODE_FAILURE;
5471  LRETURN;
5472  }
5474  continue;
5475  }
5476  else
5477  {
5478  p_ctx->pool_type = ((options & NI_SCALER_FLAG_IO) && (options & NI_SCALER_FLAG_PC)) ?
5479  (options & NI_SCALER_FLAG_P2) : (p_ctx->pool_type);
5480  break;
5481  }
5482  }
5483 
5484  if (NI_RETCODE_SUCCESS != retval)
5485  {
5486  ni_log2(p_ctx, NI_LOG_ERROR,
5487  "ERROR: ni_nvme_send_admin_cmd failed: "
5488  "blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n",
5489  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5490  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n", __func__);
5492  }
5493 
5494 END:
5495 
5496  ni_aligned_free(p_data);
5497  return retval;
5498 }
5499 
5500 /*!******************************************************************************
5501  * \brief config a frame in the scaler
5502  *
5503  * \param[in] p_ctx pointer to session context
5504  * \param[in] p_cfg pointer to frame config
5505  *
5506  * \return NI_RETCODE_INVALID_PARAM
5507  * NI_RETCODE_ERROR_INVALID_SESSION
5508  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5509  * NI_RETCODE_ERROR_MEM_ALOC
5510  *******************************************************************************/
5512  ni_frame_config_t *p_cfg)
5513 {
5516  uint32_t dataLen;
5517  uint32_t ui32LBA = 0;
5518 
5519  /* Round up to nearest 4096 bytes */
5520  dataLen =
5522  dataLen = dataLen & 0xFFFFF000;
5523 
5524  if (!p_ctx || !p_cfg)
5525  {
5526  return NI_RETCODE_INVALID_PARAM;
5527  }
5528 
5529  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5530  {
5531  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5532  __func__);
5534  }
5535 
5536  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5537  {
5538  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5539  NI_ERRNO, __func__);
5541  }
5542 
5543  memset(p_data, 0x00, dataLen);
5544 
5545  p_data->picture_width = p_cfg->picture_width;
5546  p_data->picture_height = p_cfg->picture_height;
5547  p_data->picture_format = p_cfg->picture_format;
5548  p_data->options = p_cfg->options;
5549 
5550  p_data->rectangle_width = p_cfg->rectangle_width;
5551  p_data->rectangle_height = p_cfg->rectangle_height;
5552  p_data->rectangle_x = p_cfg->rectangle_x;
5553  p_data->rectangle_y = p_cfg->rectangle_y;
5554  p_data->rgba_color = p_cfg->rgba_color;
5555  p_data->frame_index = p_cfg->frame_index;
5556  p_data->session_id = p_cfg->session_id;
5557  p_data->output_index = p_cfg->output_index;
5558  switch (p_cfg->orientation)
5559  {
5560  case 0:
5561  case 2:
5562  case 4:
5563  case 5:
5564  p_data->orientation = p_cfg->orientation;
5565  break;
5566  case 1:
5567  p_data->orientation = 3;
5568  break;
5569  case 3:
5570  p_data->orientation = 1;
5571  break;
5572  default:
5573  ni_log2(p_ctx, NI_LOG_ERROR, "Bad orientation: %u\n", p_cfg->orientation);
5575  }
5576  bool isrgb = ((GC620_RGBA8888 == p_data->picture_format) || (GC620_BGRX8888 == p_data->picture_format) ||
5577  (GC620_ARGB8888 == p_data->picture_format) || (GC620_ABGR8888 == p_data->picture_format));
5578  if(p_data->picture_width > NI_MAX_RESOLUTION_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_HEIGHT ||
5579  p_data->picture_width < NI_MIN_RESOLUTION_WIDTH_SCALER || p_data->picture_height < NI_MIN_RESOLUTION_HEIGHT_SCALER ||
5580  ((p_data->picture_width > NI_MAX_RESOLUTION_RGBA_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_RGBA_HEIGHT) && isrgb))
5581  {
5582  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", p_data->picture_width, p_data->picture_height, p_data->picture_format);
5583  ni_aligned_free(p_data);
5585  }
5586 
5587  ni_log2(p_ctx, NI_LOG_DEBUG,
5588  "Session=0x%x: Dev config frame: FrameIndex=%u; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d; O=%d\n",
5589  p_ctx->session_id, p_cfg->frame_index,
5590  p_data->picture_width, p_data->picture_height,
5591  p_data->picture_format, p_data->rectangle_width,
5592  p_data->rectangle_height, p_data->rectangle_x,
5593  p_data->rectangle_y, p_data->orientation);
5594 
5597 
5598  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5599  p_data, dataLen, ui32LBA);
5600  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5601  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5602  if (NI_RETCODE_SUCCESS != retval)
5603  {
5604  ni_log2(p_ctx, NI_LOG_ERROR,
5605  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
5606  ", hw_id, %u, xcoder_inst_id: %d\n",
5607  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5608 
5609  ni_log2(p_ctx, NI_LOG_ERROR,
5610  "ERROR ni_scaler_config(): nvme command failed!\n");
5612  }
5613 
5614 END:
5615 
5616  ni_aligned_free(p_data);
5617  return retval;
5618 }
5619 
5620 /*!******************************************************************************
5621  * \brief config multiple frames in the scaler
5622  *
5623  * \param[in] p_ctx pointer to session context
5624  * \param[in] p_cfg_in pointer to input frame config array
5625  * \param[in] numInCfgs number of input frame configs in the p_cfg array
5626  * \param[in] p_cfg_out pointer to output frame config
5627  *
5628  * \return NI_RETCODE_INVALID_PARAM
5629  * NI_RETCODE_ERROR_INVALID_SESSION
5630  * NI_RETCODE_ERROR_NVME_CMD_FAILED
5631  * NI_RETCODE_ERROR_MEM_ALOC
5632  *******************************************************************************/
5634  ni_frame_config_t p_cfg_in[],
5635  int numInCfgs,
5636  ni_frame_config_t *p_cfg_out)
5637 {
5639  ni_instance_mgr_allocation_info_t *p_data, *p_data_orig;
5640  uint32_t dataLen;
5641  uint32_t ui32LBA = 0;
5642  int i;
5643 
5644  /* Round up to nearest 4096 bytes */
5645  dataLen =
5646  sizeof(ni_instance_mgr_allocation_info_t) * (numInCfgs + 1) + NI_MEM_PAGE_ALIGNMENT - 1;
5647  dataLen = dataLen & 0xFFFFF000;
5648 
5649  if (!p_ctx || (!p_cfg_in && numInCfgs))
5650  {
5651  return NI_RETCODE_INVALID_PARAM;
5652  }
5653 
5654  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
5655  {
5656  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5657  __func__);
5659  }
5660 
5661  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
5662  {
5663  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5664  NI_ERRNO, __func__);
5666  }
5667 
5668  memset(p_data, 0x00, dataLen);
5669 
5670  p_data_orig = p_data;
5671 
5672  for (i = 0; i < numInCfgs; i++)
5673  {
5674  p_data->picture_width = p_cfg_in[i].picture_width;
5675  p_data->picture_height = p_cfg_in[i].picture_height;
5676  p_data->picture_format = p_cfg_in[i].picture_format;
5677  p_data->options = p_cfg_in[i].options & ~NI_SCALER_FLAG_IO;
5678 
5679  p_data->rectangle_width = p_cfg_in[i].rectangle_width;
5680  p_data->rectangle_height = p_cfg_in[i].rectangle_height;
5681  p_data->rectangle_x = p_cfg_in[i].rectangle_x;
5682  p_data->rectangle_y = p_cfg_in[i].rectangle_y;
5683  p_data->rgba_color = p_cfg_in[i].rgba_color;
5684  p_data->frame_index = p_cfg_in[i].frame_index;
5685  p_data->session_id = p_cfg_in[i].session_id;
5686  p_data->output_index = p_cfg_in[i].output_index;
5687 
5688  ni_log2(p_ctx, NI_LOG_DEBUG,
5689  "Session=0x%x: Dev in config frame %d: FrameIndex=%u; Session=0x%x; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5690  p_ctx->session_id, i,
5691  p_data->frame_index, p_data->session_id,
5692  p_data->picture_width, p_data->picture_height,
5693  p_data->picture_format, p_data->rectangle_width,
5694  p_data->rectangle_height, p_data->rectangle_x, p_data->rectangle_y);
5695 
5696  p_data++;
5697  }
5698 
5699  if (p_cfg_out)
5700  {
5701  p_data->picture_width = p_cfg_out->picture_width;
5702  p_data->picture_height = p_cfg_out->picture_height;
5703  p_data->picture_format = p_cfg_out->picture_format;
5704  p_data->options = p_cfg_out->options | NI_SCALER_FLAG_IO;
5705 
5706  p_data->rectangle_width = p_cfg_out->rectangle_width;
5707  p_data->rectangle_height = p_cfg_out->rectangle_height;
5708  p_data->rectangle_x = p_cfg_out->rectangle_x;
5709  p_data->rectangle_y = p_cfg_out->rectangle_y;
5710  p_data->rgba_color = p_cfg_out->rgba_color;
5711  p_data->frame_index = p_cfg_out->frame_index;
5712 
5713  ni_log2(p_ctx, NI_LOG_DEBUG,
5714  "Session=0x%x: Dev out config frame: FrameIndex=%u; W=%d; H=%d; C=%d; RW=%d; RH=%d; RX=%d; RY=%d\n",
5715  p_ctx->session_id, p_data->frame_index,
5716  p_data->picture_width, p_data->picture_height,
5717  p_data->picture_format, p_data->rectangle_width,
5718  p_data->rectangle_height, p_data->rectangle_x,
5719  p_data->rectangle_y);
5720  }
5721  if(p_data->picture_width > NI_MAX_RESOLUTION_WIDTH || p_data->picture_height > NI_MAX_RESOLUTION_HEIGHT || ((p_data->picture_width > NI_MAX_RESOLUTION_RGBA_WIDTH || p_data->picture_height> NI_MAX_RESOLUTION_RGBA_HEIGHT) && ((GC620_RGBA8888 == p_data->picture_format) || (GC620_BGRX8888 == p_data->picture_format) || (GC620_ARGB8888 == p_data->picture_format) || (GC620_ABGR8888 == p_data->picture_format))))
5722  {
5723  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", p_data->picture_width, p_data->picture_height, p_data->picture_format);
5724  ni_aligned_free(p_data);
5726  }
5727 
5730 
5731  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
5732  p_data_orig, dataLen, ui32LBA);
5733  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
5734  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
5735  if (NI_RETCODE_SUCCESS != retval)
5736  {
5737  ni_log2(p_ctx, NI_LOG_ERROR,
5738  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
5739  ", hw_id, %u, xcoder_inst_id: %d\n",
5740  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
5741 
5742  ni_log2(p_ctx, NI_LOG_ERROR,
5743  "ERROR ni_scaler_config(): nvme command failed!\n");
5745  }
5746 
5747 END:
5748 
5749  ni_aligned_free(p_data_orig);
5750  return retval;
5751 }
5752 
5753 /*!******************************************************************************
5754  * \brief Query a particular xcoder instance to get GeneralStatus data
5755  *
5756  * \param ni_session_context_t p_ctx - xcoder Context
5757  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
5758  * \param ni_instance_mgr_general_status_t *out - Struct preallocated from the caller where the
5759  * resulting data will be placed
5760  *
5761  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5762  *******************************************************************************/
5764 {
5765  void* p_buffer = NULL;
5766  int retval = NI_RETCODE_SUCCESS;
5767  uint32_t ui32LBA = 0;
5769 
5770  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5771 
5772  if ((!p_ctx) || (!p_gen_status))
5773  {
5774  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5775  __func__);
5776  retval = NI_RETCODE_INVALID_PARAM;
5777  LRETURN;
5778  }
5779 
5780  if (!IS_XCODER_DEVICE_TYPE(device_type))
5781  {
5782  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
5783  __func__, device_type);
5784  retval = NI_RETCODE_INVALID_PARAM;
5785  LRETURN;
5786  }
5787 
5788  ui32LBA = QUERY_GENERAL_GET_STATUS_R(device_type);
5789 
5790  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
5791  {
5792  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5793  NI_ERRNO, __func__);
5794  retval = NI_RETCODE_ERROR_MEM_ALOC;
5795  LRETURN;
5796  }
5797 
5798  memset(p_buffer, 0, dataLen);
5799 
5800  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
5801  {
5802  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
5804  LRETURN;
5805  }
5806 
5807  //No need to flip the bytes since the datastruct has only uint8_t datatypes
5808  memcpy((void*)p_gen_status, p_buffer, sizeof(ni_instance_mgr_general_status_t));
5809 
5810  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): model_load:%u qc:%d percent:%d\n", __func__,
5811  p_gen_status->fw_model_load, p_gen_status->cmd_queue_count,
5812  p_gen_status->process_load_percent);
5813 END:
5814 
5815  ni_aligned_free(p_buffer);
5816  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5817 
5818  return retval;
5819 }
5820 
5821 /*!******************************************************************************
5822  * \brief Query a particular xcoder instance to get DetailStatus data
5823  *
5824  * \param ni_session_context_t p_ctx - xcoder Context
5825  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
5826  * \param ni_instance_mgr_detail_status_t *out - Struct preallocated from the caller where the
5827  * resulting data will be placed
5828  *
5829  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5830  *******************************************************************************/
5831 int ni_query_detail_status(ni_session_context_t* p_ctx, ni_device_type_t device_type, void* p_detail_status, int ver)
5832 {
5833  void* p_buffer = NULL;
5834  int retval = NI_RETCODE_SUCCESS;
5835  uint32_t ui32LBA = 0;
5836  uint32_t dataLen = 0;
5837  uint32_t copyLen = 0;
5838  if(ver == 0)
5839  {
5840  ui32LBA = QUERY_DETAIL_GET_STATUS_R(device_type);
5842  dataLen = ((copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
5843  }
5844  else if(ver == 1)
5845  {
5846  ui32LBA = QUERY_DETAIL_GET_STATUS_V1_R(device_type);
5847  copyLen = sizeof(ni_instance_mgr_detail_status_v1_t);
5848  dataLen = (copyLen + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT * NI_MEM_PAGE_ALIGNMENT;
5849  }
5850  else
5851  {
5852  retval = NI_RETCODE_INVALID_PARAM;
5853  LRETURN;
5854  }
5855 
5856  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5857 
5858  if ((!p_ctx) || (!p_detail_status))
5859  {
5860  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5861  __func__);
5862  retval = NI_RETCODE_INVALID_PARAM;
5863  LRETURN;
5864  }
5865 
5866  if (!IS_XCODER_DEVICE_TYPE(device_type))
5867  {
5868  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
5869  __func__, device_type);
5870  retval = NI_RETCODE_INVALID_PARAM;
5871  LRETURN;
5872  }
5873 
5874  if (ni_posix_memalign((void **)&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
5875  {
5876  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5877  NI_ERRNO, __func__);
5878  retval = NI_RETCODE_ERROR_MEM_ALOC;
5879  LRETURN;
5880  }
5881 
5882  memset(p_buffer, 0, dataLen);
5883 
5884  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
5885  {
5886  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
5888  LRETURN;
5889  }
5890 
5891  //No need to flip the bytes since the datastruct has only uint8_t datatypes
5892  memcpy(p_detail_status, p_buffer, copyLen);
5893 
5894 END:
5895 
5896  ni_aligned_free(p_buffer);
5897  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5898 
5899  return retval;
5900 }
5901 
5902 /*!******************************************************************************
5903  * \brief Query a particular xcoder instance to get Stream Info data
5904  *
5905  * \param ni_session_context_t p_ctx - xcoder Context
5906  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
5907  * \param ni_instance_mgr_stream_info_t *out - Struct preallocated from the caller where the
5908  * resulting data will be placed
5909  *
5910  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
5911  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
5912  *******************************************************************************/
5914 {
5915  void* p_buffer = NULL;
5917  uint32_t ui32LBA = 0;
5919 
5920  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
5921 
5922  if ((!p_ctx) || (!p_stream_info))
5923  {
5924  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
5925  __func__);
5926  retval = NI_RETCODE_INVALID_PARAM;
5927  LRETURN;
5928  }
5929 
5930  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
5931  NI_DEVICE_TYPE_ENCODER == device_type))
5932  {
5933  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
5934  __func__, device_type);
5935  retval = NI_RETCODE_INVALID_PARAM;
5936  LRETURN;
5937  }
5938 
5939  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
5940  {
5941  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
5942  __func__);
5944  LRETURN;
5945  }
5946 
5947  ui32LBA = QUERY_INSTANCE_STREAM_INFO_R(p_ctx->session_id, device_type);
5948 
5949  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
5950  {
5951  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
5952  NI_ERRNO, __func__);
5953  retval = NI_RETCODE_ERROR_MEM_ALOC;
5954  LRETURN;
5955  }
5956  memset(p_buffer, 0, dataLen);
5957 
5958  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
5959  {
5960  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
5962  LRETURN;
5963  }
5964 
5965  memcpy((void*)p_stream_info, p_buffer, sizeof(ni_instance_mgr_stream_info_t));
5966 
5967  //flip the bytes to host order
5968  p_stream_info->picture_width = ni_htons(p_stream_info->picture_width);
5969  p_stream_info->picture_height = ni_htons(p_stream_info->picture_height);
5970  p_stream_info->frame_rate = ni_htons(p_stream_info->frame_rate);
5971  p_stream_info->is_flushed = ni_htons(p_stream_info->is_flushed);
5972  p_stream_info->transfer_frame_stride = ni_htons(p_stream_info->transfer_frame_stride);
5973  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): pix_format = %d\n", __func__,
5974  p_stream_info->pix_format); //temp
5975 
5976 END:
5977 
5978  ni_aligned_free(p_buffer);
5979  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
5980 
5981  return retval;
5982 }
5983 
5984 /*!*****************************************************************************
5985  * \brief Query a particular session to get the stats info
5986  *
5987  * \param ni_session_context_t p_ctx - xcoder Context
5988  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
5989  * \param ni_session_stats_t *out - Struct preallocated from the
5990  * caller where the resulting data will be placed
5991  *
5992  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
5993  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED
5994  * on failure
5995  ******************************************************************************/
5997  ni_device_type_t device_type,
5998  ni_session_stats_t *p_session_stats, int rc,
5999  int opcode)
6000 {
6002  void* p_buffer = NULL;
6003  uint64_t session_timestamp;
6004  uint32_t ui32LBA = 0;
6005  uint32_t dataLen = ((sizeof(ni_session_stats_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6006 
6007  ni_device_type_t xc_device_type =
6008  (device_type != NI_DEVICE_TYPE_UPLOAD ? device_type :
6010 
6011  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6012 
6013  if ((!p_ctx) || (!p_session_stats))
6014  {
6015  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6016  __func__);
6017  retval = NI_RETCODE_INVALID_PARAM;
6018  LRETURN;
6019  }
6020 
6021  if (!IS_XCODER_DEVICE_TYPE(xc_device_type))
6022  {
6023  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6024  __func__, device_type);
6025  retval = NI_RETCODE_INVALID_PARAM;
6026  LRETURN;
6027  }
6028 
6029  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6030  {
6031  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6032  __func__);
6034  LRETURN;
6035  }
6036 
6037  ui32LBA = QUERY_SESSION_STATS_R(p_ctx->session_id, xc_device_type);
6038 
6039  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6040  {
6041  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6042  NI_ERRNO, __func__);
6043  retval = NI_RETCODE_ERROR_MEM_ALOC;
6044  LRETURN;
6045  }
6046  memset(p_buffer, 0, dataLen);
6047 
6048  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6049  // with the command, the session id would remain invalid.
6050  // If the last command is processed successfully in session manager, the session id would become valid.
6051  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
6052  (uint16_t)NI_INVALID_SESSION_ID;
6053 
6054  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA)
6055  < 0)
6056  {
6057  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): read command Failed\n", __func__);
6059  LRETURN;
6060  }
6061 
6062  memcpy((void*)p_session_stats, p_buffer, sizeof(ni_session_stats_t));
6063 
6064  // flip the bytes to host order
6065  // all query commands are guaranteed success once a session is opened
6066  p_session_stats->ui16SessionId = ni_htons(p_session_stats->ui16SessionId);
6067  p_session_stats->ui16ErrorCount = ni_htons(p_session_stats->ui16ErrorCount);
6068  p_session_stats->ui32LastTransactionId =
6069  ni_htonl(p_session_stats->ui32LastTransactionId);
6070  p_session_stats->ui32LastTransactionCompletionStatus =
6071  ni_htonl(p_session_stats->ui32LastTransactionCompletionStatus);
6072  p_session_stats->ui32LastErrorTransactionId = ni_htonl(p_session_stats->ui32LastErrorTransactionId);
6073  p_session_stats->ui32LastErrorStatus = ni_htonl(p_session_stats->ui32LastErrorStatus);
6074  p_session_stats->ui32Session_timestamp_high = ni_htonl(p_session_stats->ui32Session_timestamp_high);
6075  p_session_stats->ui32Session_timestamp_low = ni_htonl(p_session_stats->ui32Session_timestamp_low);
6076 
6077  session_timestamp = p_session_stats->ui32Session_timestamp_high;
6078  session_timestamp <<= 32;
6079  session_timestamp |= p_session_stats->ui32Session_timestamp_low;
6080 
6081  // get the session timestamp when open session
6082  // check the timestamp during transcoding
6083  if ((p_ctx->session_timestamp != session_timestamp) &&
6084  (ni_xcoder_resource_recovery != p_session_stats->ui32LastErrorStatus))
6085  // if VPU recovery, the session timestamp will be reset.
6086  {
6087  p_session_stats->ui32LastErrorStatus =
6089  ni_log2(p_ctx, NI_LOG_DEBUG, "instance id invalid:%u, timestamp:%" PRIu64 ", "
6090  "query timestamp:%" PRIu64 "\n", p_ctx->session_id,
6091  p_ctx->session_timestamp, session_timestamp);
6092  }
6093 
6094  // check rc here, if rc != NI_RETCODE_SUCCESS, it means that last read/write command failed
6095  // failures may be link layer errors, such as physical link errors or ERROR_WRITE_PROTECT in windows.
6096  if (NI_RETCODE_SUCCESS != rc)
6097  {
6098  ni_log2(p_ctx, NI_LOG_ERROR, "%s():last command Failed: rc %d\n", __func__, rc);
6099  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): session id %u ts %lu hw_id %d device_type %u codec_format %u frame_num %lu pkt_num %lu "
6100  "ready_to_close %u session_run_state %d active_video_width %u active_video_height %u\n",
6101  __func__,
6102  p_ctx->session_id,
6103  p_ctx->session_timestamp,
6104  p_ctx->hw_id,
6105  p_ctx->device_type,
6106  p_ctx->codec_format,
6107  p_ctx->frame_num,
6108  p_ctx->pkt_num,
6109  p_ctx->ready_to_close,
6110  p_ctx->session_run_state,
6111  p_ctx->active_video_width,
6112  p_ctx->active_video_height);
6113 
6114 #if __linux__ || __APPLE__
6115 #ifndef _ANDROID
6116 #ifndef DISABLE_BACKTRACE_PRINT
6117  ni_print_backtrace(); // log backtrace
6118 #endif
6119 #endif
6120 #endif
6121 
6122  p_session_stats->ui32LastTransactionCompletionStatus =
6124  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_NVME_CMD_FAILED;
6126  } else if (p_ctx->session_id != p_session_stats->ui16SessionId)
6127  {
6128  uint64_t ct = ni_gettime_ns();
6129  uint64_t dt = ct - p_ctx->last_access_time;
6130  ni_log2(p_ctx, NI_LOG_ERROR,
6131  "%s(): device 0x%" PRIx64 " last command Failed due to wrong "
6132  "session ID. Expected 0x%x, got 0x%x keep alive last access "
6133  "time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6134  (int64_t)p_ctx->blk_io_handle, p_ctx->session_id,
6135  p_session_stats->ui16SessionId, p_ctx->last_access_time, ct);
6136  if (dt > 1000000000)
6137  {
6138  ni_log2(p_ctx, NI_LOG_ERROR,
6139  "%s():long delay between last command dt = %" PRId64 " ns, "
6140  "process was possibly blocked.\n", __func__, dt);
6141  }
6142  p_session_stats->ui32LastErrorStatus = NI_RETCODE_ERROR_INVALID_SESSION;
6143 
6144  // Mark session id to INVALID so that all commands afterward are blocked
6146  }
6147 
6148  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6149  __func__, p_session_stats->ui16ErrorCount,
6150  p_session_stats->ui32LastTransactionCompletionStatus,
6151  p_session_stats->ui32LastErrorStatus);
6152 
6153 END:
6154 
6155  ni_aligned_free(p_buffer);
6156  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6157 
6158  return retval;
6159 }
6160 
6161 /*!******************************************************************************
6162  * \brief Query a particular xcoder instance to get End of Output data
6163  *
6164  * \param ni_session_context_t p_ctx - xcoder Context
6165  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6166  * \param InstMgrStreamComp *out - Struct preallocated from the caller where the
6167  * resulting data will be placed
6168  *
6169  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_MEM_ALOC
6170  * or NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6171  *******************************************************************************/
6173 {
6174  void* p_buffer = NULL;
6176  uint32_t ui32LBA = 0;
6178 
6179  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6180 
6181  if (!p_ctx || !p_stream_complete)
6182  {
6183  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6184  __func__);
6185  retval = NI_RETCODE_INVALID_PARAM;
6186  LRETURN;
6187  }
6188 
6189  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6190  NI_DEVICE_TYPE_ENCODER == device_type))
6191  {
6192  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6193  __func__, device_type);
6194  retval = NI_RETCODE_INVALID_PARAM;
6195  LRETURN;
6196  }
6197 
6198  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6199  {
6200  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6201  __func__);
6203  LRETURN;
6204  }
6205 
6206  ui32LBA = QUERY_INSTANCE_EOS_R(p_ctx->session_id, device_type);
6207 
6208  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6209  {
6210  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
6211  retval = NI_RETCODE_ERROR_MEM_ALOC;
6212  LRETURN;
6213  }
6214 
6215  memset(p_buffer, 0, dataLen);
6216 
6217  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6218  {
6219  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6221  LRETURN;
6222  }
6223 
6224  memcpy((void*)p_stream_complete, p_buffer, sizeof(ni_instance_mgr_stream_complete_t));
6225 
6226  //flip the bytes to host order
6227  p_stream_complete->is_flushed = ni_htons(p_stream_complete->is_flushed);
6228 
6229 END:
6230 
6231  ni_aligned_free(p_buffer);
6232  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6233 
6234  return retval;
6235 }
6236 
6237 static const char* ni_get_device_type_str(int type)
6238 {
6239  if (type < NI_DEVICE_TYPE_DECODER || type > NI_DEVICE_TYPE_AI)
6240  {
6241  return "Invalid device type";
6242  }
6243  return g_device_type_str[type];
6244 }
6245 
6246 static void
6247 ni_parse_session_statistic_info(ni_session_context_t *p_ctx,
6248  ni_session_statistic_t *p_session_statistic,
6249  void *p_buffer)
6250 {
6251  memcpy((void *)p_session_statistic, p_buffer,
6252  sizeof(ni_session_statistic_t));
6253 
6254  //flip the bytes to host order
6255  p_session_statistic->ui32RdBufAvailSize =
6256  ni_htonl(p_session_statistic->ui32RdBufAvailSize);
6257  p_session_statistic->ui32WrBufAvailSize =
6258  ni_htonl(p_session_statistic->ui32WrBufAvailSize);
6259 
6260  p_session_statistic->ui32FramesInput =
6261  ni_htonl(p_session_statistic->ui32FramesInput);
6262  p_session_statistic->ui32FramesBuffered =
6263  ni_htonl(p_session_statistic->ui32FramesBuffered);
6264  p_session_statistic->ui32FramesCompleted =
6265  ni_htonl(p_session_statistic->ui32FramesCompleted);
6266  p_session_statistic->ui32FramesOutput =
6267  ni_htonl(p_session_statistic->ui32FramesOutput);
6268  p_session_statistic->ui32FramesDropped =
6269  ni_htonl(p_session_statistic->ui32FramesDropped);
6270  p_session_statistic->ui32InstErrors =
6271  ni_htonl(p_session_statistic->ui32InstErrors);
6272 
6273  p_session_statistic->ui16SessionId =
6274  ni_htons(p_session_statistic->ui16SessionId);
6275  p_session_statistic->ui16ErrorCount =
6276  ni_htons(p_session_statistic->ui16ErrorCount);
6277  p_session_statistic->ui32LastTransactionId =
6278  ni_htonl(p_session_statistic->ui32LastTransactionId);
6279  p_session_statistic->ui32LastTransactionCompletionStatus =
6280  ni_htonl(p_session_statistic->ui32LastTransactionCompletionStatus);
6281  p_session_statistic->ui32LastErrorTransactionId =
6282  ni_htonl(p_session_statistic->ui32LastErrorTransactionId);
6283  p_session_statistic->ui32LastErrorStatus =
6284  ni_htonl(p_session_statistic->ui32LastErrorStatus);
6285  p_session_statistic->ui32Session_timestamp_high =
6286  ni_htonl(p_session_statistic->ui32Session_timestamp_high);
6287  p_session_statistic->ui32Session_timestamp_low =
6288  ni_htonl(p_session_statistic->ui32Session_timestamp_low);
6289 
6290  // p_session_statistic->ui8AdditionalFramesDelay does not require endian conversion
6291 
6292  if (p_ctx->session_id != p_session_statistic->ui16SessionId)
6293  {
6294  uint64_t ct = ni_gettime_ns();
6295  uint64_t dt = ct - p_ctx->last_access_time;
6296  ni_log2(p_ctx, NI_LOG_ERROR,
6297  "%s(): %s device 0x%" PRIx64 " last command Failed due to wrong "
6298  "session ID. Expected 0x%x, got 0x%x w_r <%u %u> keep alive "
6299  "last access time %" PRIu64 ", current %" PRIu64 "\n", __func__,
6300  ni_get_device_type_str(p_ctx->device_type),
6301  (int64_t)p_ctx->device_handle, p_ctx->session_id,
6302  p_session_statistic->ui16SessionId,
6303  p_session_statistic->ui32WrBufAvailSize,
6304  p_session_statistic->ui32RdBufAvailSize, p_ctx->last_access_time,
6305  ct);
6306  if (dt > 1000000000)
6307  {
6308  ni_log2(p_ctx, NI_LOG_ERROR,
6309  "%s():long delay between last command dt = %" PRId64 " ns, "
6310  "process was possibly blocked.\n", __func__, dt);
6311  }
6312  p_session_statistic->ui32LastErrorStatus =
6314  //Mark session id to INVALID so that all commands afterward are blocked
6316  } else
6317  {
6318  //Acknowledge that total error count here
6319  }
6320 
6321  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): error count %u last rc 0x%x inst_err_no 0x%x\n",
6322  __func__, p_session_statistic->ui16ErrorCount,
6323  p_session_statistic->ui32LastTransactionCompletionStatus,
6324  p_session_statistic->ui32LastErrorStatus);
6325 }
6326 
6327 /*!*****************************************************************************
6328  * \brief Query a particular xcoder session to get session statistics
6329  *
6330  * \param ni_session_context_t p_ctx - xcoder Context
6331  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6332  * \param ni_session_statistic_t*out - Struct preallocated from the caller
6333  * where the resulting data will be placed
6334  *
6335  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6336  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6337  * failure
6338  ******************************************************************************/
6341  ni_device_type_t device_type,
6342  ni_session_statistic_t *p_session_statistic)
6343 {
6344  void *p_buffer = NULL;
6346  uint32_t ui32LBA = 0;
6347  uint32_t dataLen =
6348  ((sizeof(ni_session_statistic_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6351 
6352  if (!p_ctx || !p_session_statistic)
6353  {
6354  ni_log2(p_ctx, NI_LOG_ERROR,
6355  "ERROR: %s() passed parameters are null!, return\n", __func__);
6356  retval = NI_RETCODE_INVALID_PARAM;
6357  LRETURN;
6358  }
6359 
6361  "65") < 0)
6362  {
6363  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() not supported on device with FW api version < 6.5\n", __func__);
6365  }
6366 
6367  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6368  NI_DEVICE_TYPE_ENCODER == device_type ||
6369  NI_DEVICE_TYPE_AI == device_type))
6370  {
6371  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6372  __func__, device_type);
6373  retval = NI_RETCODE_INVALID_PARAM;
6374  LRETURN;
6375  }
6376 
6377  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6378  {
6379  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6380  __func__);
6382  LRETURN;
6383  }
6384 
6385  ui32LBA = QUERY_INSTANCE_CUR_STATUS_INFO_R(p_ctx->session_id, device_type);
6386 
6387  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6388  {
6389  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6390  NI_ERRNO, __func__);
6391  retval = NI_RETCODE_ERROR_MEM_ALOC;
6392  LRETURN;
6393  }
6394  memset(p_buffer, 0, dataLen);
6395 
6396  // Set session ID to be invalid. In case, the last command fails because the invalid session ID was submitted
6397  // with the command, the session id would remain invalid.
6398  // If the Last command is processed successfully in session manager, the session id would become valid.
6399  ((ni_session_statistic_t *)p_buffer)->ui16SessionId =
6400  (uint16_t)NI_INVALID_SESSION_ID;
6401 
6403  p_buffer, dataLen, ui32LBA) < 0)
6404  {
6405  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): NVME command Failed\n", __func__);
6406  p_session_statistic->ui32LastTransactionCompletionStatus =
6408  p_session_statistic->ui32LastErrorStatus =
6411  LRETURN;
6412  }
6413 
6414  ni_parse_session_statistic_info(p_ctx, p_session_statistic, p_buffer);
6415  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
6416  {
6418  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, %s return.\n",
6419  __func__, ni_get_device_type_str(p_ctx->device_type));
6420  LRETURN;
6421  }
6422  p_ctx->session_statistic = *p_session_statistic;
6423 
6424 END:
6425  ni_aligned_free(p_buffer);
6426  ni_log2(p_ctx, NI_LOG_TRACE, "%s():exit\n", __func__);
6427 
6428  return retval;
6429 }
6430 /*!*****************************************************************************
6431  * \brief Query a particular xcoder instance to get buffer/data Info data
6432  *
6433  * \param ni_session_context_t p_ctx - xcoder Context
6434  * \param ni_instance_buf_info_rw_type_t rw_type
6435  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6436  * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
6437  * where the resulting data will be placed
6438  *
6439  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6440  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6441  * failure
6442  ******************************************************************************/
6445  ni_device_type_t device_type,
6446  ni_instance_buf_info_t *p_inst_buf_info)
6447 {
6448  void* p_buffer = NULL;
6450  uint32_t ui32LBA = 0;
6451  uint32_t dataLen =
6452  ((sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) /
6455 
6456  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6457 
6458  if (!p_ctx || !p_inst_buf_info)
6459  {
6460  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6461  __func__);
6462  retval = NI_RETCODE_INVALID_PARAM;
6463  LRETURN;
6464  }
6465 
6466  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6467  NI_DEVICE_TYPE_ENCODER == device_type ||
6468  NI_DEVICE_TYPE_SCALER == device_type ||
6469  NI_DEVICE_TYPE_AI == device_type))
6470  {
6471  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6472  __func__, device_type);
6473  retval = NI_RETCODE_INVALID_PARAM;
6474  LRETURN;
6475  }
6476 
6477  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6478  {
6479  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6480  __func__);
6482  LRETURN;
6483  }
6484 
6485  if (INST_BUF_INFO_RW_READ == rw_type)
6486  {
6487  ui32LBA = QUERY_INSTANCE_RBUFF_SIZE_R(p_ctx->session_id, device_type);
6488  }
6489  else if (INST_BUF_INFO_RW_WRITE == rw_type)
6490  {
6491  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R(p_ctx->session_id, device_type);
6492  }
6493  else if(INST_BUF_INFO_RW_WRITE_BY_EP == rw_type)
6494  {
6496  "65") >= 0)
6497  {
6498  ui32LBA = QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(p_ctx->session_id, device_type);
6499  }
6500  else
6501  {
6503  LRETURN;
6504  }
6505  }
6506  else if (INST_BUF_INFO_RW_UPLOAD == rw_type)
6507  {
6508  ui32LBA = QUERY_INSTANCE_UPLOAD_ID_R(p_ctx->session_id, device_type);
6509  } else if (INST_BUF_INFO_R_ACQUIRE == rw_type)
6510  {
6511  ui32LBA = QUERY_INSTANCE_ACQUIRE_BUF(p_ctx->session_id, device_type);
6512  } else if (INST_BUF_INFO_RW_READ_BUSY == rw_type)
6513  {
6514  ui32LBA =
6515  QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6516  } else if (INST_BUF_INFO_RW_WRITE_BUSY == rw_type)
6517  {
6518  ui32LBA =
6519  QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(p_ctx->session_id, device_type);
6520  } else if (INST_BUF_INFO_RW_READ_BY_AI == rw_type)
6521  {
6522  ui32LBA = QUERY_INSTANCE_AI_INFO_R(p_ctx->session_id, device_type);
6523  } else
6524  {
6525  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown query type %d, return\n",
6526  __func__, rw_type);
6527  retval = NI_RETCODE_INVALID_PARAM;
6528  LRETURN;
6529  }
6530 
6531  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
6532  {
6533  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6534  NI_ERRNO, __func__);
6535  retval = NI_RETCODE_ERROR_MEM_ALOC;
6536  LRETURN;
6537  }
6538 
6539  memset(p_buffer, 0, dataLen);
6540 
6541  if (ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, dataLen, ui32LBA) < 0)
6542  {
6543  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6545  LRETURN;
6546  }
6547 
6548  memcpy((void*)p_inst_buf_info, p_buffer, sizeof(ni_instance_buf_info_t));
6549 
6550  p_inst_buf_info->buf_avail_size = ni_htonl(p_inst_buf_info->buf_avail_size);
6551 
6552 END:
6553 
6554  ni_aligned_free(p_buffer);
6555  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6556 
6557  return retval;
6558 }
6559 
6560 /*!*****************************************************************************
6561  * \brief Configure the read/write pipe for a session to control its behavior
6562  *
6563  * \param ni_session_context_t p_ctx - xcoder Context
6564  * \param ni_session_config_rw_t rw_type
6565  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6566  * \param uint8_t enable
6567  * \param uint8_t hw_action
6568  * \param uint16_t frame_id
6569  *
6570  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
6571  * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
6572  * failure
6573  ******************************************************************************/
6576  uint8_t enable, uint8_t hw_action,
6577  uint16_t frame_id)
6578 {
6580  uint32_t ui32LBA = 0;
6581  void * p_buffer = NULL;
6582  uint32_t buffer_size = 0;
6583  ni_session_config_rw_t * rw_config = NULL;
6584 
6585  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6586 
6587  if (!p_ctx)
6588  {
6589  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6590  __func__);
6591  retval = NI_RETCODE_INVALID_PARAM;
6592  LRETURN;
6593  }
6594 
6595  if (!((SESSION_READ_CONFIG == rw_type) || (SESSION_WRITE_CONFIG == rw_type)))
6596  {
6597  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6598  __func__, rw_type);
6599  retval = NI_RETCODE_INVALID_PARAM;
6600  LRETURN;
6601  }
6602 
6604  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
6605  {
6606  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6607  NI_ERRNO, __func__);
6608  retval = NI_RETCODE_ERROR_MEM_ALOC;
6609  LRETURN;
6610  }
6611  memset(p_buffer, 0, buffer_size);
6612  rw_config = (ni_session_config_rw_t *)p_buffer;
6613  rw_config->ui8Enable = enable;
6614  rw_config->ui8HWAccess = hw_action;
6615  switch(rw_type)
6616  {
6617  case SESSION_READ_CONFIG:
6618  rw_config->uHWAccessField.ui16ReadFrameId = frame_id;
6619  break;
6620  case SESSION_WRITE_CONFIG:
6621  rw_config->uHWAccessField.ui16WriteFrameId = frame_id;
6622  break;
6623  default:
6624  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6625  __func__, rw_type);
6626  retval = NI_RETCODE_INVALID_PARAM;
6627  LRETURN;
6628  }
6629 
6630  switch(rw_type)
6631  {
6632  case SESSION_READ_CONFIG:
6633  ui32LBA = CONFIG_SESSION_Read_W(p_ctx->session_id);
6634  break;
6635  case SESSION_WRITE_CONFIG:
6636  ui32LBA = CONFIG_SESSION_Write_W(p_ctx->session_id);
6637  break;
6638  default:
6639  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown config type %d, return\n",
6640  __func__, rw_type);
6641  retval = NI_RETCODE_INVALID_PARAM;
6642  LRETURN;
6643  }
6644  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
6645  {
6646  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6648  LRETURN;
6649  }
6650 
6651 END:
6652 
6653  ni_aligned_free(p_buffer);
6654  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6655 
6656  return retval;
6657 }
6658 
6659 /*!******************************************************************************
6660  * \brief Send a p_config command for Start Of Stream
6661  *
6662  * \param ni_session_context_t p_ctx - xcoder Context
6663  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6664  *
6665  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION. NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6666  *******************************************************************************/
6668 {
6670  uint32_t ui32LBA = 0;
6671 
6672  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6673 
6674  if (!p_ctx)
6675  {
6676  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6677  __func__);
6678  retval = NI_RETCODE_INVALID_PARAM;
6679  LRETURN;
6680  }
6681 
6682  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6683  NI_DEVICE_TYPE_ENCODER == device_type))
6684  {
6685  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6686  __func__, device_type);
6687  retval = NI_RETCODE_INVALID_PARAM;
6688  LRETURN;
6689  }
6690 
6691  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6692  {
6693  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6694  __func__);
6696  LRETURN;
6697  }
6698 
6699  ui32LBA = CONFIG_INSTANCE_SetSOS_W(p_ctx->session_id, device_type);
6700 
6701  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_ctx->p_all_zero_buf, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
6702  {
6703  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6705  }
6706 
6707 END:
6708 
6709  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6710  return retval;
6711 }
6712 
6713 /*!******************************************************************************
6714  * \brief Send a p_config command for End Of Stream
6715  *
6716  * \param ni_session_context_t p_ctx - xcoder Context
6717  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6718  *
6719  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6720  *******************************************************************************/
6722 {
6724  uint32_t ui32LBA = 0;
6725 
6726  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6727 
6728  if (!p_ctx)
6729  {
6730  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6731  __func__);
6732  retval = NI_RETCODE_INVALID_PARAM;
6733  LRETURN;
6734  }
6735 
6736  if (! (NI_DEVICE_TYPE_DECODER == device_type ||
6737  NI_DEVICE_TYPE_ENCODER == device_type))
6738  {
6739  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6740  __func__, device_type);
6741  retval = NI_RETCODE_INVALID_PARAM;
6742  LRETURN;
6743  }
6744 
6745  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6746  {
6747  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6748  __func__);
6750  LRETURN;
6751  }
6752 
6753  ui32LBA = CONFIG_INSTANCE_SetEOS_W(p_ctx->session_id, device_type);
6754 
6755  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_ctx->p_all_zero_buf, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
6756  {
6757  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6759  }
6760 
6761 END:
6762 
6763  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6764 
6765  return retval;
6766 }
6767 
6768 /*!******************************************************************************
6769  * \brief Send a p_config command to flush the stream
6770  *
6771  * \param ni_session_context_t p_ctx - xcoder Context
6772  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6773  *
6774  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6775  *******************************************************************************/
6777 {
6779  uint32_t ui32LBA = 0;
6780 
6781  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6782 
6783  if (!p_ctx)
6784  {
6785  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6786  __func__);
6787  retval = NI_RETCODE_INVALID_PARAM;
6788  LRETURN;
6789  }
6790 
6791  if (NI_DEVICE_TYPE_DECODER != device_type)
6792  {
6793  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6794  __func__, device_type);
6795  retval = NI_RETCODE_INVALID_PARAM;
6796  LRETURN;
6797  }
6798 
6799  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6800  {
6801  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6802  __func__);
6804  LRETURN;
6805  }
6806 
6807  ui32LBA = CONFIG_INSTANCE_Flush_W(p_ctx->session_id, device_type);
6808  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
6810  ui32LBA);
6811  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
6812  p_ctx->device_type, p_ctx->hw_id,
6813  &(p_ctx->session_id), OPT_1);
6814  CHECK_VPU_RECOVERY(retval);
6815 
6816 END:
6817 
6818  if (NI_RETCODE_SUCCESS != retval)
6819  {
6821  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed with %d\n", __func__, retval);
6822  }
6823 
6824  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6825 
6826  return retval;
6827 }
6828 
6829 /*!******************************************************************************
6830  * \brief Send a p_config command to set the length for the incoming write packet
6831  *
6832  * \param ni_session_context_t p_ctx - xcoder Context
6833  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6834  * \param
6835  *
6836  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6837  *******************************************************************************/
6839 {
6841  uint32_t ui32LBA = 0;
6842  void * p_buffer = NULL;
6843  uint32_t buffer_size = 0;
6844 
6845  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6846 
6847  if (!p_ctx)
6848  {
6849  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6850  __func__);
6851  retval = NI_RETCODE_INVALID_PARAM;
6852  LRETURN;
6853  }
6854 
6855  if (!(NI_DEVICE_TYPE_DECODER == device_type ||
6856  NI_DEVICE_TYPE_ENCODER == device_type))
6857  {
6858  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Unknown device type %d, return\n",
6859  __func__, device_type);
6860  retval = NI_RETCODE_INVALID_PARAM;
6861  LRETURN;
6862  }
6863 
6864  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6865  {
6866  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6867  __func__);
6869  LRETURN;
6870  }
6871 
6872  buffer_size = ((sizeof(len) + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
6873  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
6874  {
6875  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6876  NI_ERRNO, __func__);
6877  retval = NI_RETCODE_ERROR_MEM_ALOC;
6878  LRETURN;
6879  }
6880  memset(p_buffer, 0, buffer_size);
6881  memcpy(p_buffer, &len, sizeof(len));
6882 
6883  ui32LBA = CONFIG_INSTANCE_SetPktSize_W(p_ctx->session_id, device_type);
6884 
6885  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
6886  {
6887  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6889  }
6890 
6891 END:
6892 
6893  ni_aligned_free(p_buffer);
6894  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6895 
6896  return retval;
6897 }
6898 
6899 /*!******************************************************************************
6900  * \brief Send a p_config command to inform encoder sequence change
6901  *
6902  * \param ni_session_context_t p_ctx - xcoder Context
6903  * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
6904  * \param ni_resolution_t p_resolution - sequence change resolution
6905  *
6906  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6907  *******************************************************************************/
6909 {
6911  uint32_t ui32LBA = 0;
6912  void * p_buffer = NULL;
6913  uint32_t buffer_size = 0;
6914 
6915  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6916 
6917  if (!p_ctx)
6918  {
6919  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6920  __func__);
6921  retval = NI_RETCODE_INVALID_PARAM;
6922  LRETURN;
6923  }
6924 
6925  if (!(NI_DEVICE_TYPE_ENCODER == device_type))
6926  {
6927  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Seq Change not supported for device type %d, return\n", device_type);
6928  retval = NI_RETCODE_INVALID_PARAM;
6929  LRETURN;
6930  }
6931 
6932  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6933  {
6934  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6935  __func__);
6937  LRETURN;
6938  }
6939 
6941  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), buffer_size))
6942  {
6943  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
6944  NI_ERRNO, __func__);
6945  retval = NI_RETCODE_ERROR_MEM_ALOC;
6946  LRETURN;
6947  }
6948  memset(p_buffer, 0, buffer_size);
6949  memcpy(p_buffer, p_resolution, sizeof(ni_resolution_t));
6950 
6951  ui32LBA = CONFIG_INSTANCE_SetSeqChange_W(p_ctx->session_id, device_type);
6952 
6953  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_buffer, buffer_size, ui32LBA) < 0)
6954  {
6955  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
6957  }
6958 
6959 END:
6960 
6961  ni_aligned_free(p_buffer);
6962  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
6963 
6964  return retval;
6965 }
6966 
6967 /*!******************************************************************************
6968  * \brief Send a p_config command to configure encoding parameters.
6969  *
6970  * \param ni_session_context_t p_ctx - xcoder Context
6971  *
6972  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
6973  *******************************************************************************/
6975 {
6976  void* p_encoder_config = NULL;
6977  ni_encoder_config_t* p_cfg = NULL;
6978  uint32_t buffer_size = sizeof(ni_encoder_config_t);
6980  uint32_t ui32LBA = 0;
6981  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
6982 
6983  if (!p_ctx)
6984  {
6985  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
6986  __func__);
6987  retval = NI_RETCODE_INVALID_PARAM;
6988  LRETURN;
6989  }
6990 
6991  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
6992  {
6993  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
6994  __func__);
6996  LRETURN;
6997  }
6998 
6999  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7000  if (ni_posix_memalign(&p_encoder_config, sysconf(_SC_PAGESIZE), buffer_size))
7001  {
7002  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7003  NI_ERRNO, __func__);
7004  retval = NI_RETCODE_ERROR_MEM_ALOC;
7005  LRETURN;
7006  }
7007  memset(p_encoder_config, 0, buffer_size);
7008 
7009  ni_set_custom_template(p_ctx, p_encoder_config, p_ctx->p_session_config);
7010  retval = ni_validate_custom_template(p_ctx, p_encoder_config, p_ctx->p_session_config, p_ctx->param_err_msg, sizeof(p_ctx->param_err_msg));
7011  if (NI_RETCODE_PARAM_WARN == retval)
7012  {
7013  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: ni_validate_custom_template() . %s\n", p_ctx->param_err_msg);
7014  fflush(stdout);
7015  }
7016  else if (NI_RETCODE_SUCCESS != retval)
7017  {
7018  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_validate_custom_template() failed. %s\n", p_ctx->param_err_msg);
7019  fflush(stdout);
7020  retval = NI_RETCODE_INVALID_PARAM;
7021  LRETURN;
7022  }
7023  //configure the session here
7025 
7026  //Flip the bytes!! any param 16bits in size need ni_htons, 32bit need ni_htonl
7027  p_cfg = (ni_encoder_config_t*)p_encoder_config;
7028  p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
7029  p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
7030  p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
7031  p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
7032  p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
7033  p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
7034  p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
7035  p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
7036  p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
7037  p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
7038  p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
7039  p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
7040  p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
7041  p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
7042  p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
7043  p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
7044  p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
7045  p_cfg->ui16HDR10MaxLight = ni_htons(p_cfg->ui16HDR10MaxLight);
7046  p_cfg->ui16HDR10AveLight = ni_htons(p_cfg->ui16HDR10AveLight);
7047  p_cfg->ui16gdrDuration = ni_htons(p_cfg->ui16gdrDuration);
7048  p_cfg->ui32ltrRefInterval = ni_htonl(p_cfg->ui32ltrRefInterval);
7049  p_cfg->i32ltrRefQpOffset = ni_htonl(p_cfg->i32ltrRefQpOffset);
7050  p_cfg->ui32ltrFirstGap = ni_htonl(p_cfg->ui32ltrFirstGap);
7051  p_cfg->i32tolCtbRcInter = ni_htonl(p_cfg->i32tolCtbRcInter);
7052  p_cfg->i32tolCtbRcIntra = ni_htonl(p_cfg->i32tolCtbRcIntra);
7053  p_cfg->i16bitrateWindow = ni_htons(p_cfg->i16bitrateWindow);
7054  p_cfg->ui16hdr10_dx0 = ni_htons(p_cfg->ui16hdr10_dx0);
7055  p_cfg->ui16hdr10_dy0 = ni_htons(p_cfg->ui16hdr10_dy0);
7056  p_cfg->ui16hdr10_dx1 = ni_htons(p_cfg->ui16hdr10_dx1);
7057  p_cfg->ui16hdr10_dy1 = ni_htons(p_cfg->ui16hdr10_dy1);
7058  p_cfg->ui16hdr10_dx2 = ni_htons(p_cfg->ui16hdr10_dx2);
7059  p_cfg->ui16hdr10_dy2 = ni_htons(p_cfg->ui16hdr10_dy2);
7060  p_cfg->ui16hdr10_wx = ni_htons(p_cfg->ui16hdr10_wx);
7061  p_cfg->ui16hdr10_wy = ni_htons(p_cfg->ui16hdr10_wy);
7062  p_cfg->ui32hdr10_maxluma = ni_htonl(p_cfg->ui32hdr10_maxluma);
7063  p_cfg->ui32hdr10_minluma = ni_htonl(p_cfg->ui32hdr10_minluma);
7064  p_cfg->ui32lumaLinesize = ni_htons(p_cfg->ui32lumaLinesize);
7065  p_cfg->ui32chromaLinesize = ni_htons(p_cfg->ui32chromaLinesize);
7066 
7067  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
7068  p_encoder_config, buffer_size, ui32LBA);
7069  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
7070  p_ctx->device_type, p_ctx->hw_id,
7071  &(p_ctx->session_id), OPT_1);
7072  if (NI_RETCODE_SUCCESS != retval)
7073  {
7074  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7075  //Close the session since we can't configure it
7076  retval = ni_encoder_session_close(p_ctx, 0);
7077  if (NI_RETCODE_SUCCESS != retval)
7078  {
7079  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7080  }
7081 
7083  }
7084 
7085 END:
7086 
7087  ni_aligned_free(p_encoder_config);
7088  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7089  return retval;
7090 }
7091 
7092 /*!******************************************************************************
7093  * \brief Send a p_config command to configure encoding p_frame parameters.
7094  *
7095  * \param ni_session_context_t p_ctx - xcoder Context
7096  * \param ni_encoder_frame_params_t * params - pointer to the encoder ni_encoder_frame_params_t struct
7097  *
7098  * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
7099  *******************************************************************************/
7101 {
7103  uint32_t buffer_size = sizeof(ni_encoder_frame_params_t);
7105  uint32_t ui32LBA = 0;
7106 
7107  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
7108 
7109  if (!p_ctx || !p_params)
7110  {
7111  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
7112  __func__);
7113  return NI_RETCODE_INVALID_PARAM;
7114  }
7115 
7116  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
7117  {
7118  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
7119  __func__);
7121  }
7122 
7123  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
7124  if (ni_posix_memalign((void **)&p_cfg, sysconf(_SC_PAGESIZE), buffer_size))
7125  {
7126  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
7127  NI_ERRNO, __func__);
7129  }
7130 
7131  //configure the session here
7133 
7134  //Flip the bytes!!
7135  p_cfg->force_picture_type = ni_htons(p_params->force_picture_type);
7136  p_cfg->data_format = ni_htons(p_params->data_format);
7137  p_cfg->picture_type = ni_htons(p_params->picture_type);
7138  p_cfg->video_width = ni_htons(p_params->video_width);
7139  p_cfg->video_height = ni_htons(p_params->video_height);
7140  p_cfg->timestamp = ni_htonl(p_params->timestamp);
7141 
7142  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_cfg,
7143  buffer_size, ui32LBA) < 0)
7144  {
7145  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %d, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7146  //Close the session since we can't configure it
7147  retval = ni_encoder_session_close(p_ctx, 0);
7148  if (NI_RETCODE_SUCCESS != retval)
7149  {
7150  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %d, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
7151  }
7152 
7154  }
7155 
7156  ni_aligned_free(p_cfg);
7157  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
7158 
7159  return retval;
7160 }
7161 
7162 /*!******************************************************************************
7163  * \brief Get info from received p_frame
7164  *
7165  * \param
7166  *
7167  * \return
7168  *******************************************************************************/
7169 int ni_create_frame(ni_frame_t* p_frame, uint32_t read_length, uint64_t* p_frame_offset, bool is_hw_frame)
7170 {
7171  uint32_t rx_size =
7172  read_length; //get the length since its the only thing in DW10 now
7173 
7174  if (!p_frame || !p_frame_offset)
7175  {
7176  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7177  __func__);
7178  return NI_RETCODE_INVALID_PARAM;
7179  }
7180 
7181  *p_frame_offset = 0;
7182 
7183  unsigned int metadata_size = NI_FW_META_DATA_SZ -
7185  unsigned int video_data_size = p_frame->data_len[0] + p_frame->data_len[1] +
7186  p_frame->data_len[2] + ((is_hw_frame) ? p_frame->data_len[3] : 0);
7187  ni_log(NI_LOG_DEBUG, "rx_size = %d metadataSize = %d\n", rx_size,
7188  metadata_size);
7189 
7190  p_frame->p_custom_sei_set = NULL;
7191 
7192  if (rx_size == metadata_size)
7193  {
7194  video_data_size = 0;
7195  }
7196 
7197  if (rx_size > video_data_size)
7198  {
7199  ni_metadata_dec_frame_t *p_meta =
7200  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
7201  video_data_size);
7202 
7203  *p_frame_offset = p_meta->metadata_common.ui64_data.frame_offset;
7204  rx_size -= metadata_size;
7205  p_frame->crop_top = p_meta->metadata_common.crop_top;
7206  p_frame->crop_bottom = p_meta->metadata_common.crop_bottom;
7207  p_frame->crop_left = p_meta->metadata_common.crop_left;
7208  p_frame->crop_right = p_meta->metadata_common.crop_right;
7209  p_frame->ni_pict_type = p_meta->metadata_common.frame_type;
7210 
7211  p_frame->video_width = p_meta->metadata_common.frame_width;
7212  p_frame->video_height = p_meta->metadata_common.frame_height;
7213 
7214  ni_log(
7215  NI_LOG_DEBUG,
7216  "%s: [metadata] cropRight=%u, cropLeft=%u, "
7217  "cropBottom=%u, cropTop=%u, frame_offset=%" PRIu64 ", pic=%ux%u, "
7218  "pict_type=%d, crop=%ux%u, sei header: 0x%0x number %u size %u\n",
7219  __func__, p_frame->crop_right, p_frame->crop_left,
7220  p_frame->crop_bottom, p_frame->crop_top,
7222  p_meta->metadata_common.frame_width,
7223  p_meta->metadata_common.frame_height, p_frame->ni_pict_type,
7224  p_frame->crop_right - p_frame->crop_left,
7225  p_frame->crop_bottom - p_frame->crop_top, p_meta->sei_header,
7226  p_meta->sei_number, p_meta->sei_size);
7227 
7228  p_frame->sei_total_len = 0;
7229  p_frame->sei_cc_offset = 0;
7230  p_frame->sei_cc_len = 0;
7235  p_frame->sei_hdr_plus_offset = 0;
7236  p_frame->sei_hdr_plus_len = 0;
7237  p_frame->sei_user_data_unreg_offset = 0;
7238  p_frame->sei_user_data_unreg_len = 0;
7239 
7240  if (p_meta->sei_number)
7241  {
7242 #if 1 // QUADRA_SEI_FMT
7243  ni_log(NI_LOG_DEBUG, "ui32SeiHeader 0x%x ui16SeiNumber %d ui16SeiSize %d SEI 0x%02x%02x\n",
7244  p_meta->sei_header, p_meta->sei_number, p_meta->sei_size,
7245  *((uint8_t*)p_meta + metadata_size),
7246  *((uint8_t*)p_meta + metadata_size+1));
7247 
7248  { // retrieve sei from new format
7249  uint16_t ui16SeiProcessed = 0;
7250  ni_sei_header_t * pEntryHeader = &p_meta->first_sei_header;
7251  uint32_t ui32Offset = 0;
7252  uint32_t ui32Size;
7253 
7254  rx_size -= p_meta->sei_size;
7255 
7256  do
7257  {
7258  ui16SeiProcessed++;
7259 
7260  if (pEntryHeader->status)
7261  {
7262  ui32Size = pEntryHeader->size;
7263  }
7264  else
7265  {
7266  ui32Size = 0;
7267  }
7268 
7269  ni_log(NI_LOG_DEBUG, "SEI #%x st %d size %d ty %d sz/of %u/%u 0x%02x%02x\n",
7270  ui16SeiProcessed, pEntryHeader->status,
7271  pEntryHeader->size, pEntryHeader->type, ui32Size,
7272  ui32Offset,
7273  *((uint8_t *)p_meta + metadata_size + ui32Offset),
7274  *((uint8_t *)p_meta + metadata_size + ui32Offset + 1));
7275 
7276  // - if multiple entries with same SEI type/subtype, only the last entry is saved;
7277  // consider to use sei_offset[] array instead of explicit sei_*_offset
7278  // - user_data_unreg (UDU) and custom sei passthru via HW and SW respectively
7279  // thus custom SEI is not processed here as it is by SW.
7280 
7281  switch(pEntryHeader->type)
7282  {
7283  case 4: //HEVC_SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35
7284  if (ui32Size)
7285  {
7286  uint8_t *ptr = (uint8_t*)p_meta + metadata_size + ui32Offset;
7287  if(ptr[0] == NI_HDR10P_SEI_BYTE0 && ptr[1] == NI_HDR10P_SEI_BYTE1 &&
7288  ptr[2] == NI_HDR10P_SEI_BYTE2 && ptr[3] == NI_HDR10P_SEI_BYTE3 &&
7289  ptr[4] == NI_HDR10P_SEI_BYTE4 && ptr[5] == NI_HDR10P_SEI_BYTE5)
7290  {
7291  p_frame->sei_hdr_plus_len = ui32Size;
7292  p_frame->sei_hdr_plus_offset =
7293  video_data_size + metadata_size + ui32Offset;
7294 
7295  p_frame->sei_total_len += ui32Size;
7296 
7297  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n",
7298  __func__, p_frame->sei_hdr_plus_len,
7299  p_frame->sei_hdr_plus_offset);
7300  }
7301  else if(ptr[0] == NI_CC_SEI_BYTE0 && ptr[1] == NI_CC_SEI_BYTE1 &&
7302  ptr[2] == NI_CC_SEI_BYTE2 && ptr[3] == NI_CC_SEI_BYTE3 &&
7303  ptr[4] == NI_CC_SEI_BYTE4 && ptr[5] == NI_CC_SEI_BYTE5 &&
7304  ptr[6] == NI_CC_SEI_BYTE6)
7305  {
7306  // Found CC data
7307  // number of 3 byte close captions is bottom 5 bits of
7308  // 9th byte of T35 payload
7309  // uint32_t ui32CCSize = (ptr[8] & 0x1F) * 3; // avoid overwriting ui32CCSize
7310 
7311  // return close caption data offset and length, and
7312  // skip past 10 header bytes to close caption data
7313  p_frame->sei_cc_len = (ptr[8] & 0x1F) * 3; // ui32CCSize;
7314  p_frame->sei_cc_offset = video_data_size + metadata_size
7315  + ui32Offset + 10;
7316 
7317  p_frame->sei_total_len += p_frame->sei_cc_len;
7318 
7320  "%s: close caption size %u ,"
7321  "offset %u = video size %u meta size %d off "
7322  " %u + 10\n",
7323  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7324  video_data_size, metadata_size, ui32Offset);
7325  }
7326  else
7327  {
7329  "%s: unsupported T35; type %u size %u status %u "
7330  "offset %u\n",
7331  __func__, pEntryHeader->type, pEntryHeader->size,
7332  pEntryHeader->status, ui32Offset);
7333  }
7334  }
7335  else
7336  {
7338  "Error %s: T35 (missing payload); type %u size %u "
7339  "status %u offset %u\n",
7340  __func__, pEntryHeader->type, pEntryHeader->size,
7341  pEntryHeader->status, ui32Offset);
7342  }
7343  break;
7344 
7345  case 5: // HEVC_SEI_TYPE_USER_DATA_UNREGISTERED
7346  // set offset now so len=0 will signify an error if this SEI is dropped
7347  p_frame->sei_user_data_unreg_offset = video_data_size + metadata_size + ui32Offset;
7348  if (ui32Size)
7349  {
7350  p_frame->sei_user_data_unreg_len = ui32Size;
7351  p_frame->sei_total_len += ui32Size;
7352  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32Size);
7353  }
7354  else
7355  {
7356  p_frame->sei_user_data_unreg_len = 0; // in case there are multiple UDU SEI entries
7358  "Error %s: User Data Unreg dropped (missing payload); "
7359  "type %u size %u status %u offset %u\n",
7360  __func__, pEntryHeader->type, pEntryHeader->size,
7361  pEntryHeader->status, ui32Offset);
7362  }
7363  break;
7364 
7365  case 137: //HEVC_SEI_TYPE_MASTERING_DISPLAY_INFO
7366  if (ui32Size)
7367  {
7368  p_frame->sei_hdr_mastering_display_color_vol_len = ui32Size;
7370  video_data_size + metadata_size + ui32Offset;
7371 
7372  p_frame->sei_total_len += ui32Size;
7375  *)((uint8_t *)p_meta + metadata_size + ui32Offset);
7376 
7377  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7378  ni_ntohs(pColourVolume->display_primaries[0][0]),
7379  ni_ntohs(pColourVolume->display_primaries[0][1]));
7380  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7381  ni_ntohs(pColourVolume->display_primaries[1][0]),
7382  ni_ntohs(pColourVolume->display_primaries[1][1]));
7383  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7384  ni_ntohs(pColourVolume->display_primaries[2][0]),
7385  ni_ntohs(pColourVolume->display_primaries[2][1]));
7386 
7387  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7388  ni_ntohs(pColourVolume->white_point_x),
7389  ni_ntohs(pColourVolume->white_point_y));
7390  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7391  ni_ntohl(pColourVolume->max_display_mastering_luminance),
7392  ni_ntohl(pColourVolume->min_display_mastering_luminance));
7393  }
7394  else
7395  {
7397  "Error %s: mastering display info dropped (missing "
7398  "payload); type %u size %u status %u offset %u\n",
7399  __func__, pEntryHeader->type, pEntryHeader->size,
7400  pEntryHeader->status, ui32Offset);
7401  }
7402  break;
7403 
7404  case 144: //HEVC_SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
7405  if (ui32Size)
7406  {
7407  p_frame->sei_hdr_content_light_level_info_len = ui32Size;
7409  video_data_size + metadata_size + ui32Offset;
7410 
7411  p_frame->sei_total_len += ui32Offset;
7412 
7415  (uint8_t*)p_meta + metadata_size + ui32Offset);
7416 
7417  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7418  ni_ntohs(pLightLevel->max_content_light_level),
7419  ni_ntohs(pLightLevel->max_pic_average_light_level));
7420  }
7421  else
7422  {
7424  "Error %s: content light level info dropped (missing "
7425  "payload); type %u size %u status %u offset %u\n",
7426  __func__, pEntryHeader->type, pEntryHeader->size,
7427  pEntryHeader->status, ui32Offset);
7428  }
7429  break;
7430 
7431  case 200: // Custom SEI not included in HEVC_SEI_Type
7432  if (ui32Size)
7433  {
7434  p_frame->vui_len = ui32Size;
7435  p_frame->vui_offset =
7436  video_data_size + metadata_size + ui32Offset;
7437  p_frame->sei_total_len += ui32Size;
7438  }
7439  break;
7440 
7441  default:
7443  "Error %s: SEI message dropped (unsupported - check "
7444  "decoder SEI bitmap settings);"
7445  " type %u size %u status %u offset %u payload bytes %u\n",
7446  __func__, pEntryHeader->type, pEntryHeader->size,
7447  pEntryHeader->status, ui32Offset, ui32Size);
7448  break;
7449  }
7450  ui32Offset += ui32Size;
7451  pEntryHeader = (ni_sei_header_t *)((uint8_t*)p_meta + metadata_size + ui32Offset);
7452  ui32Offset += sizeof(ni_sei_header_t);
7453  } while (ui32Offset <= p_meta->sei_size && ui16SeiProcessed < p_meta->sei_number);
7454 
7455  if (p_meta->sei_number != ui16SeiProcessed)
7456  {
7457  ni_log(
7458  NI_LOG_ERROR,
7459  "Error %s: number of SEI messages reported %u != processed %u\n",
7460  __func__, p_meta->sei_number, ui16SeiProcessed);
7461  }
7462  }
7463 
7464 #else // QUADRA_SEI_FMT
7465 
7466  // ni_assert(p_meta->sei_header);
7467  // else // backward compatibility
7468  {
7469  ni_sei_user_data_entry_t *pEntry;
7470  uint32_t ui32CCOffset = 0, ui32CCSize = 0;
7471 
7472  rx_size -= p_meta->sei_size;
7473 
7474  pEntry = (ni_sei_user_data_entry_t *)((uint8_t*)p_meta + metadata_size);
7475 
7476  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_HDR10_PLUS, pEntry,
7477  &ui32CCOffset, &ui32CCSize))
7478  {
7479  p_frame->sei_hdr_plus_len = ui32CCSize;
7480  p_frame->sei_hdr_plus_offset =
7481  video_data_size + metadata_size + ui32CCOffset;
7482 
7483  p_frame->sei_total_len += ui32CCSize;
7484 
7485  ni_log(NI_LOG_DEBUG, "%s: hdr10+ size=%u hdr10+ offset=%u\n", __func__,
7486  p_frame->sei_hdr_plus_len, p_frame->sei_hdr_plus_offset);
7487  }
7488  else
7489  {
7490  ni_log(NI_LOG_DEBUG, "%s: hdr+ NOT found in meta data!\n", __func__);
7491  }
7492 
7493  if (find_t35_sei(p_meta->sei_header, NI_T35_SEI_CLOSED_CAPTION, pEntry,
7494  &ui32CCOffset, &ui32CCSize))
7495  {
7496  uint8_t *ptr;
7497  // Found CC data at pEntry + ui32CCOffset
7498  ptr = (uint8_t*)pEntry + ui32CCOffset;
7499  // number of 3 byte close captions is bottom 5 bits of
7500  // 9th byte of T35 payload
7501  ui32CCSize = (ptr[8] & 0x1F) * 3;
7502 
7503  // return close caption data offset and length, and
7504  // skip past 10 header bytes to close caption data
7505  p_frame->sei_cc_len = ui32CCSize;
7506  p_frame->sei_cc_offset = video_data_size + metadata_size
7507  + ui32CCOffset + 10;
7508 
7509  p_frame->sei_total_len += p_frame->sei_cc_len;
7510 
7512  "%s: close caption size %u ,"
7513  "offset %u = video size %u meta size %u off "
7514  " %u + 10\n",
7515  __func__, p_frame->sei_cc_len, p_frame->sei_cc_offset,
7516  video_data_size, metadata_size, ui32CCOffset);
7517  }
7518  else
7519  {
7520  ni_log(NI_LOG_DEBUG, "%s: close caption NOT found in meta data!\n",
7521  __func__);
7522  }
7523 
7524  if (find_sei(p_meta->sei_header, pEntry,
7526  &ui32CCOffset, &ui32CCSize))
7527  {
7528  p_frame->sei_hdr_mastering_display_color_vol_len = ui32CCSize;
7530  video_data_size + metadata_size + ui32CCOffset;
7531 
7532  p_frame->sei_total_len += ui32CCSize;
7533 
7534  ni_dec_mastering_display_colour_volume_t* pColourVolume =
7535  (ni_dec_mastering_display_colour_volume_t*)((uint8_t*)pEntry + ui32CCOffset);
7536 
7537  ni_log(NI_LOG_DEBUG, "Display Primaries x[0]=%u y[0]=%u\n",
7538  pColourVolume->display_primaries_x[0],
7539  pColourVolume->display_primaries_y[0]);
7540  ni_log(NI_LOG_DEBUG, "Display Primaries x[1]=%u y[1]=%u\n",
7541  pColourVolume->display_primaries_x[1],
7542  pColourVolume->display_primaries_y[1]);
7543  ni_log(NI_LOG_DEBUG, "Display Primaries x[2]=%u y[2]=%u\n",
7544  pColourVolume->display_primaries_x[2],
7545  pColourVolume->display_primaries_y[2]);
7546 
7547  ni_log(NI_LOG_DEBUG, "White Point x=%u y=%u\n",
7548  pColourVolume->white_point_x,
7549  pColourVolume->white_point_y);
7550  ni_log(NI_LOG_DEBUG, "Display Mastering Lum, Max=%u Min=%u\n",
7551  pColourVolume->max_display_mastering_luminance, pColourVolume->min_display_mastering_luminance);
7552  }
7553  if (find_sei(p_meta->sei_header, pEntry,
7555  &ui32CCOffset, &ui32CCSize))
7556  {
7557  p_frame->sei_hdr_content_light_level_info_len = ui32CCSize;
7559  video_data_size + metadata_size + ui32CCOffset;
7560 
7561  p_frame->sei_total_len += ui32CCSize;
7562 
7563  ni_content_light_level_info_t* pLightLevel =
7564  (ni_content_light_level_info_t*)((uint8_t*)pEntry + ui32CCOffset);
7565  ni_log(NI_LOG_DEBUG, "Max Content Light level=%u Max Pic Avg Light Level=%u\n",
7566  pLightLevel->max_content_light_level, pLightLevel->max_pic_average_light_level);
7567  }
7568 
7569  if (find_sei(p_meta->sei_header, pEntry,
7571  &ui32CCOffset, &ui32CCSize) ||
7572  find_sei(p_meta->sei_header, pEntry,
7574  &ui32CCOffset, &ui32CCSize))
7575  {
7576  p_frame->sei_user_data_unreg_len = ui32CCSize;
7577  p_frame->sei_user_data_unreg_offset =
7578  video_data_size + metadata_size + ui32CCOffset;
7579 
7580  p_frame->sei_total_len += ui32CCSize;
7581 
7582  ni_log(NI_LOG_DEBUG, "User Data Unreg size = %u\n", ui32CCSize);
7583  }
7584  }
7585 #endif // QUADRA_SEI_FMT
7586  if (0 == p_frame->sei_total_len)
7587  {
7588  ni_log(NI_LOG_DEBUG, "Warning retrieved 0 supported SEI !\n");
7589  }
7590  }
7591  }
7592 
7593  p_frame->dts = NI_NOPTS_VALUE;
7594  p_frame->pts = NI_NOPTS_VALUE;
7595  //p_frame->end_of_stream = isEndOfStream;
7596  p_frame->start_of_stream = 0;
7597 
7598  if (rx_size == 0)
7599  {
7600  p_frame->data_len[0] = 0;
7601  p_frame->data_len[1] = 0;
7602  p_frame->data_len[2] = 0;
7603  p_frame->data_len[3] = 0;
7604  }
7605 
7606  ni_log(NI_LOG_DEBUG, "received [0x%08x] data size: %d, end of stream=%u\n",
7607  read_length, rx_size, p_frame->end_of_stream);
7608 
7609  return rx_size;
7610 }
7611 
7612 /*!******************************************************************************
7613  * \brief Get info from received xcoder capability
7614  *
7615  * \param
7616  *
7617  * \return
7618  *******************************************************************************/
7620 {
7621  int i, total_types = 0, total_modules = 0;
7622  ni_nvme_identity_t *p_id_data = (ni_nvme_identity_t *)p_data;
7623 
7624  COMPILE_ASSERT(sizeof(p_cap->xcoder_cnt) <= sizeof(p_id_data->xcoder_cnt) &&
7626  sizeof(p_id_data->xcoder_cnt[0]) ==
7627  sizeof(p_cap->xcoder_cnt));
7628 
7629  if (!p_cap || !p_data)
7630  {
7631  ni_log(NI_LOG_ERROR, "ERROR: %s(): Null pointer parameters passed\n",
7632  __func__);
7633  LRETURN;
7634  }
7635 
7636  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
7637  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
7638  {
7639  if (g_device_in_ctxt)
7640  {
7642  "ERROR: Previously in context device got an invalid vendor ID 0x%X SSVID 0x%X. Netint "
7643  "ID 0x%X. Retrying\n",
7644  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
7645  //print some other fields as a test to see if they are invalid too
7646  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
7647  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
7648  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
7649  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
7650  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
7651  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
7652  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
7653  p_id_data->xcoder_num_elements);
7654 
7655  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
7656  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
7657  if (ni_nvme_send_read_cmd(g_dev_handle, event_handle, p_data,
7658  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
7659  {
7660  LRETURN;
7661  }
7662  if ((p_id_data->ui16Vid != NETINT_PCI_VENDOR_ID) ||
7663  (p_id_data->ui16Ssvid != NETINT_PCI_VENDOR_ID))
7664  {
7666  "ERROR: %s(): Retry got an invalid vendor ID too 0x%X SSVID "
7667  "0x%X!\n",
7668  __func__, p_id_data->ui16Vid, p_id_data->ui16Ssvid);
7669  //print some other fields as a test to see if they are invalid too
7670  ni_log(NI_LOG_ERROR, "Model Number: %.*s\n",
7671  (int)sizeof(p_id_data->ai8Sn), p_id_data->ai8Sn);
7672  ni_log(NI_LOG_ERROR, "Serial Number: %.*s\n",
7673  (int)sizeof(p_id_data->ai8Mn), p_id_data->ai8Mn);
7674  ni_log(NI_LOG_ERROR, "Firmware Revision: %.*s\n",
7675  (int)sizeof(p_id_data->ai8Fr), p_id_data->ai8Fr);
7676  ni_log(NI_LOG_ERROR, "xcoder_num_elements: %d\n",
7677  p_id_data->xcoder_num_elements);
7678  LRETURN;
7679  }
7680  else
7681  {
7683  "Retry got valid a vendor ID 0x%X SSVID 0x%X. Netint ID 0x%X\n",
7684  p_id_data->ui16Vid, p_id_data->ui16Ssvid, NETINT_PCI_VENDOR_ID);
7685  }
7686  }
7687  else
7688  {
7689  LRETURN;
7690  }
7691  }
7692 
7693  memcpy(p_cap->serial_number, p_id_data->ai8Sn, sizeof(p_cap->serial_number));
7694  memcpy(p_cap->model_number, p_id_data->ai8Mn, sizeof(p_cap->model_number));
7695 
7696  memset(p_cap->fw_rev, 0, sizeof(p_cap->fw_rev));
7697  memcpy(p_cap->fw_rev, p_id_data->ai8Fr, sizeof(p_cap->fw_rev));
7698  ni_log(NI_LOG_DEBUG, "F/W rev: %.*s\n", (int)sizeof(p_cap->fw_rev),
7699  p_cap->fw_rev);
7700 
7701  if (p_id_data->xcoder_num_elements)
7702  {
7703  ni_log(NI_LOG_DEBUG, "xcoder_num_elements: %d xcoder_num_devices: %d\n",
7704  p_id_data->xcoder_num_elements,
7705  p_id_data->xcoder_num_devices);
7706 
7707  for (i = 0; i < NI_DEVICE_TYPE_XCODER_MAX; i++)
7708  {
7709  if (p_id_data->xcoder_cnt[i])
7710  {
7711  total_types++;
7712  total_modules += p_id_data->xcoder_cnt[i];
7713  ni_log(NI_LOG_DEBUG, "type #%d: xcoder_cnt[%d] = %d\n", total_types, i,
7714  p_id_data->xcoder_cnt[i]);
7715  }
7716  }
7717 
7718  if (p_id_data->xcoder_num_elements != total_types ||
7719  p_id_data->xcoder_num_devices != total_modules)
7720  {
7722  "Error: mismatch; xcoder_num_elements: %d (calculated: %d) "
7723  "xcoder_num_devices: %d (calculated: %d)\n",
7724  p_id_data->xcoder_num_elements, total_types,
7725  p_id_data->xcoder_num_devices, total_modules);
7726  LRETURN;
7727  }
7728 
7730  p_cap->hw_elements_cnt = p_id_data->xcoder_num_elements;
7731  p_cap->xcoder_devices_cnt = p_id_data->xcoder_num_devices;
7732  memcpy(p_cap->xcoder_cnt, p_id_data->xcoder_cnt,
7733  NI_DEVICE_TYPE_XCODER_MAX * sizeof(p_id_data->xcoder_cnt[0]));
7734 
7736  {
7737  if (!p_id_data->xcoder_cnt[i])
7738  continue;
7739 
7740  p_cap->xcoder_devices[i].hw_id = p_id_data->xcoder_devices[i].hw_id;
7744  p_cap->xcoder_devices[i].codec_format =
7745  p_id_data->xcoder_devices[i].hw_codec_format;
7746  p_cap->xcoder_devices[i].codec_type =
7747  p_id_data->xcoder_devices[i].hw_codec_type;
7750  if (i == NI_DEVICE_TYPE_ENCODER)
7751  {
7754  }
7755  else
7756  {
7759  }
7760 
7761  p_cap->xcoder_devices[i].video_profile =
7762  p_id_data->xcoder_devices[i].hw_video_profile;
7763  p_cap->xcoder_devices[i].video_level =
7764  p_id_data->xcoder_devices[i].hw_video_level;
7765  }
7766 
7767  goto CAP_POPULATED;
7768  }
7769 
7770  p_cap->device_is_xcoder = p_id_data->device_is_xcoder;
7771  ni_log(NI_LOG_DEBUG, "device_is_xcoder: value in id cmd: %u\n",
7772  p_cap->device_is_xcoder);
7773  if (0 == p_cap->device_is_xcoder)
7774  {
7775  ni_log(NI_LOG_ERROR, "Not an xcoder device !\n");
7776 
7777  if (g_device_in_ctxt)
7778  {
7780  "ERROR: Previously in context device is not a xcoder device "
7781  "now!\n");
7782  }
7783  LRETURN;
7784  }
7785 
7786  p_cap->hw_elements_cnt = p_id_data->xcoder_num_hw;
7787 
7788  total_modules = p_cap->xcoder_cnt[NI_DEVICE_TYPE_DECODER] =
7789  p_id_data->xcoder_num_h264_decoder_hw +
7790  p_id_data->xcoder_num_h265_decoder_hw;
7791 
7793  p_id_data->xcoder_num_h264_encoder_hw +
7794  p_id_data->xcoder_num_h265_encoder_hw;
7795 
7796  total_modules += p_cap->xcoder_cnt[NI_DEVICE_TYPE_ENCODER];
7797 
7798  p_cap->xcoder_devices_cnt = total_modules;
7799 
7800  if (total_modules >= 1)
7801  {
7802  p_cap->xcoder_devices[0].hw_id = p_id_data->hw0_id;
7806  p_cap->xcoder_devices[0].codec_format = p_id_data->hw0_codec_format;
7807  p_cap->xcoder_devices[0].codec_type = p_id_data->hw0_codec_type;
7812  p_cap->xcoder_devices[0].video_profile = p_id_data->hw0_video_profile;
7813  p_cap->xcoder_devices[0].video_level = p_id_data->hw0_video_level;
7814  }
7815  if (total_modules >= 2)
7816  {
7817  p_cap->xcoder_devices[1].hw_id = p_id_data->hw1_id;
7821  p_cap->xcoder_devices[1].codec_format = p_id_data->hw1_codec_format;
7822  p_cap->xcoder_devices[1].codec_type = p_id_data->hw1_codec_type;
7827  p_cap->xcoder_devices[1].video_profile = p_id_data->hw1_video_profile;
7828  p_cap->xcoder_devices[1].video_level = p_id_data->hw1_video_level;
7829  }
7830  if (total_modules >= 3)
7831  {
7832  p_cap->xcoder_devices[2].hw_id = p_id_data->hw2_id;
7836  p_cap->xcoder_devices[2].codec_format = p_id_data->hw2_codec_format;
7837  p_cap->xcoder_devices[2].codec_type = p_id_data->hw2_codec_type;
7842  p_cap->xcoder_devices[2].video_profile = p_id_data->hw2_video_profile;
7843  p_cap->xcoder_devices[2].video_level = p_id_data->hw2_video_level;
7844  }
7845  if (total_modules >= 4)
7846  {
7847  p_cap->xcoder_devices[3].hw_id = p_id_data->hw3_id;
7851  p_cap->xcoder_devices[3].codec_format = p_id_data->hw3_codec_format;
7852  p_cap->xcoder_devices[3].codec_type = p_id_data->hw3_codec_type;
7857  p_cap->xcoder_devices[3].video_profile = p_id_data->hw3_video_profile;
7858  p_cap->xcoder_devices[3].video_level = p_id_data->hw3_video_level;
7859  }
7860 
7861 CAP_POPULATED:
7862 
7863  for (i = 0; i < NI_MAX_DEVICES_PER_HW_INSTANCE; i++)
7864  {
7865  ni_log(NI_LOG_DEBUG, "HW%d hw_id: %d\n", i, p_cap->xcoder_devices[i].hw_id);
7866  ni_log(NI_LOG_DEBUG, "HW%d max_number_of_contexts: %d\n", i,
7868  ni_log(NI_LOG_DEBUG, "HW%d max_4k_fps: %d\n", i,
7869  p_cap->xcoder_devices[i].max_4k_fps);
7870  ni_log(NI_LOG_DEBUG, "HW%d codec_format: %d\n", i,
7871  p_cap->xcoder_devices[i].codec_format);
7872  ni_log(NI_LOG_DEBUG, "HW%d codec_type: %d\n", i,
7873  p_cap->xcoder_devices[i].codec_type);
7874  ni_log(NI_LOG_DEBUG, "HW%d max_video_width: %d\n", i,
7875  p_cap->xcoder_devices[i].max_video_width);
7876  ni_log(NI_LOG_DEBUG, "HW%d max_video_height: %d\n", i,
7877  p_cap->xcoder_devices[i].max_video_height);
7878  ni_log(NI_LOG_DEBUG, "HW%d min_video_width: %d\n", i,
7879  p_cap->xcoder_devices[i].min_video_width);
7880  ni_log(NI_LOG_DEBUG, "HW%d min_video_height: %d\n", i,
7881  p_cap->xcoder_devices[i].min_video_height);
7882  ni_log(NI_LOG_DEBUG, "HW%d video_profile: %d\n", i,
7883  p_cap->xcoder_devices[i].video_profile);
7884  ni_log(NI_LOG_DEBUG, "HW%d video_level: %d\n", i,
7885  p_cap->xcoder_devices[i].video_level);
7886  }
7887 
7888  memset(p_cap->fw_branch_name, 0, sizeof(p_cap->fw_branch_name));
7889  memcpy(p_cap->fw_branch_name, p_id_data->fw_branch_name,
7890  sizeof(p_cap->fw_branch_name) - 1);
7891  ni_log(NI_LOG_DEBUG, "F/W branch name: %s\n", p_cap->fw_branch_name);
7892  memset(p_cap->fw_commit_time, 0, sizeof(p_cap->fw_commit_time));
7893  memcpy(p_cap->fw_commit_time, p_id_data->fw_commit_time,
7894  sizeof(p_cap->fw_commit_time) - 1);
7895  ni_log(NI_LOG_DEBUG, "F/W commit time: %s\n", p_cap->fw_commit_time);
7896  memset(p_cap->fw_commit_hash, 0, sizeof(p_cap->fw_commit_hash));
7897  memcpy(p_cap->fw_commit_hash, p_id_data->fw_commit_hash,
7898  sizeof(p_cap->fw_commit_hash) - 1);
7899  ni_log(NI_LOG_DEBUG, "F/W commit hash: %s\n", p_cap->fw_commit_hash);
7900  memset(p_cap->fw_build_time, 0, sizeof(p_cap->fw_build_time));
7901  memcpy(p_cap->fw_build_time, p_id_data->fw_build_time,
7902  sizeof(p_cap->fw_build_time) - 1);
7903  ni_log(NI_LOG_DEBUG, "F/W build time: %s\n", p_cap->fw_build_time);
7904  memset(p_cap->fw_build_id, 0, sizeof(p_cap->fw_build_id));
7905  memcpy(p_cap->fw_build_id, p_id_data->fw_build_id,
7906  sizeof(p_cap->fw_build_id) - 1);
7907  ni_log(NI_LOG_DEBUG, "F/W build id: %s\n", p_cap->fw_build_id);
7908 
7909 END:
7910  return;
7911 }
7912 
7913 static uint32_t presetGopSize[] = {
7914  1,
7915  1,
7916  1,
7917  1,
7918  2,
7919  4,
7920  4,
7921  4,
7922  8 };
7923 
7924 static uint32_t presetGopKeyFrameFactor[] = {
7925  1,
7926  1,
7927  1,
7928  1,
7929  2,
7930  4,
7931  1,
7932  1,
7933  1 };
7934 
7935 /*!******************************************************************************
7936  * \brief insert the 32 bits of integer value at bit position pos
7937  *
7938  * \param int pos, int value
7939  *
7940  * \return void
7941  ******************************************************************************/
7942 void ni_fix_VUI(uint8_t *vui, int pos, int value)
7943 {
7944  int pos_byte = (pos/8);
7945  int pos_in_byte = pos%8;
7946  int remaining_bytes_in_current_byte = 8 - pos_in_byte;
7947 
7948  if (pos_in_byte == 0) // at beginning of the byte
7949  {
7950  vui[pos_byte] = (uint8_t)(value >> 24);
7951  vui[pos_byte+1] = (uint8_t)(value >> 16);
7952  vui[pos_byte+2] = (uint8_t)(value >> 8);
7953  vui[pos_byte+3] = (uint8_t)(value);
7954  }
7955  else
7956  {
7957  vui[pos_byte] = vui[pos_byte] + (uint8_t)(value >> (32-remaining_bytes_in_current_byte));
7958  vui[pos_byte+1] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-8));
7959  vui[pos_byte+2] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-16));
7960  vui[pos_byte+3] = (uint8_t)(value >> (32-remaining_bytes_in_current_byte-24));
7961  vui[pos_byte+4] = vui[pos_byte+4] + ((uint8_t)(value << remaining_bytes_in_current_byte));
7962  }
7963 
7964 }
7965 
7966 /*!******************************************************************************
7967 * \brief Setup all xcoder configurations with custom parameters (Rev. B)
7968 *
7969 * \param
7970 *
7971 * \return
7972 ******************************************************************************/
7974  ni_decoder_config_t *p_cfg,
7975  ni_xcoder_params_t *p_src,
7976  uint32_t max_pkt_size)
7977 {
7978  int i,j;
7979  ni_decoder_input_params_t* p_dec = NULL;
7980  if ((!p_ctx) || (!p_cfg) || (!p_src))
7981  {
7982  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
7983  __func__);
7984  return;
7985  }
7986  p_dec = &p_src->dec_input_params;
7987  int w = p_src->source_width;
7988  int h = p_src->source_height;
7989  bool shift_params = false;
7990 
7991  p_cfg->ui8HWFrame = p_dec->hwframes;
7992  p_cfg->ui8MCMode = p_dec->mcmode;
7995  p_cfg->ui8DisablePictureReordering = p_dec->decoder_low_delay > 0;
7996  p_ctx->force_low_delay = p_dec->force_low_delay;
7997  p_cfg->ui8EnablelowDelayCheck = p_dec->enable_low_delay_check == 1;
7998  p_cfg->ui32SourceWidth = w;
7999  p_cfg->ui32SourceHeight = h;
8000 
8001  if (max_pkt_size)
8002  {
8003  p_cfg->ui32MaxPktSize = max_pkt_size;
8004  } else {
8005  // p_cfg->ui32MaxPktSize = width x height x 3/2 x min compression ratio(QP=0);
8006  // set min compression ratio = 1/2, so MaxPktSize = w * h * 3/4
8007  p_cfg->ui32MaxPktSize = w * h * 3 / 4;
8008  }
8009  // packet buffer aligned to NI_MAX_PACKET_SZ(128k)
8010  p_cfg->ui32MaxPktSize =
8011  (((p_cfg->ui32MaxPktSize) / NI_MAX_PACKET_SZ) + 1) * NI_MAX_PACKET_SZ;
8012 
8013  p_cfg->fps_number =
8014  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_number;
8015  p_cfg->fps_denominator =
8016  ((ni_xcoder_params_t *)p_ctx->p_session_config)->fps_denominator;
8017  ni_log2(p_ctx, NI_LOG_INFO, "%s height %d width %d fps_number %d fps_denominator %d\n",
8018  __func__, h, w, p_cfg->fps_number, p_cfg->fps_denominator);
8019 
8020  p_cfg->asOutputConfig[0].ui8Enabled = 1; // always enabled
8021  p_cfg->asOutputConfig[1].ui8Enabled = p_dec->enable_out1;
8022  p_cfg->asOutputConfig[2].ui8Enabled = p_dec->enable_out2;
8023  if (p_cfg->asOutputConfig[2].ui8Enabled && p_cfg->asOutputConfig[1].ui8Enabled == 0)
8024  {
8025  p_cfg->asOutputConfig[1].ui8Enabled = 1;
8026  p_cfg->asOutputConfig[2].ui8Enabled = 0;
8027  shift_params = true;
8028  ni_log2(p_ctx, NI_LOG_DEBUG, "Output 2 used before output 1, Shifting output2 settings to output1 and disabling output 2\n");
8029  }
8030 
8031  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8032  {
8033  if (!shift_params || i == 0)
8034  {
8035  j = i;
8036  }
8037  else
8038  {
8039  j = (i == 1) ? 2 : 1; //swap settings
8040  }
8041  p_cfg->asOutputConfig[i].ui8Force8Bit = p_dec->force_8_bit[j];
8042  p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled = p_dec->semi_planar[j];
8043  p_cfg->asOutputConfig[i].ui8CropMode = p_dec->crop_mode[j];
8045  (uint16_t)((p_dec->crop_whxy[j][0]) & 0xFFFE);
8047  (uint16_t)((p_dec->crop_whxy[j][1]) & 0xFFFE);
8049  (uint16_t)((p_dec->crop_whxy[j][2]) & 0xFFFE);
8051  (uint16_t)((p_dec->crop_whxy[j][3]) & 0xFFFE);
8052 
8053  //Offset resized if out of bounds
8055  {
8057  }
8059  {
8061  }
8062 
8063  if (p_dec->enable_ppu_scale_adapt)
8064  {
8066  }
8067  else if (p_dec->scale_long_short_edge[j] == 1)
8068  {
8069  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 3;
8070  }
8071  else if (p_dec->scale_long_short_edge[j] == 2)
8072  {
8073  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 4;
8074  }
8075  else
8076  {
8077  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt = 0;
8078  }
8080 
8081  if (p_dec->scale_round[j] == -1)
8082  {
8083  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 1 ||
8084  p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 2)
8085  {
8086  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8087  }
8088  else
8089  {
8091  }
8092  }
8093  else if (p_dec->scale_round[j] == 0)
8094  {
8096  }
8097  else
8098  {
8099  p_cfg->asOutputConfig[i].ui8ScaleResCeil = p_dec->scale_resolution_ceil[j] - 1;
8100  }
8101 
8103  (uint16_t)((p_dec->scale_wh[j][0]+1) & 0xFFFE);
8105  (uint16_t)((p_dec->scale_wh[j][1]+1) & 0xFFFE);
8106 
8109  {
8110  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 1;
8111  }
8112  else
8113  {
8114  p_cfg->asOutputConfig[i].ui8ScaleEnabled = 0;
8115  }
8116 
8117  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt == 0 &&
8118  p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit == 1)
8119  {
8120  ni_log2(p_ctx, NI_LOG_ERROR, "WARN: set ui8EnablePpuScaleLimit to 0 when ui8EnablePpuScaleAdapt is disabled.\n");
8121  p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit = 0;
8122  }
8123  }
8124 
8126  if (p_cfg->ui8MaxExtraHwFrameCnt != 255 &&
8127  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rB") < 0)
8128  {
8129  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): maxExtraHwFrameCnt is not support for FW < 6rB\n", __func__);
8130  }
8131  p_cfg->ui8EcPolicy = p_dec->ec_policy;
8132  p_cfg->ui8EnableAdvancedEc = p_dec->enable_advanced_ec;
8133  if (p_cfg->ui8EnableAdvancedEc == 2 &&
8134  ni_cmp_fw_api_ver((char *)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rO") < 0)
8135  {
8136  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): (enableAdvancedEc == 2) is not support for FW < 6rO\n", __func__);
8137  p_cfg->ui8EnableAdvancedEc = 1;
8138  ni_log2(p_ctx, NI_LOG_INFO, "Warning %s(): reset enableAdvancedEc to %d\n", __func__, p_cfg->ui8EnableAdvancedEc);
8139  }
8141 
8142  //print it all out
8143  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HWFrame = %d\n", p_cfg->ui8HWFrame);
8144  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MCMode = %d\n", p_cfg->ui8MCMode);
8145  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8UduSeiEnabled = %d\n", p_cfg->ui8UduSeiEnabled);
8146  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16MaxSeiDataSize = %d\n", p_cfg->ui16MaxSeiDataSize);
8147  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisablePictureReordering = %d\n", p_cfg->ui8DisablePictureReordering);
8148  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled0 = %d\n", p_cfg->asOutputConfig[0].ui8Enabled);
8149  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled1 = %d\n", p_cfg->asOutputConfig[1].ui8Enabled);
8150  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8Enabled2 = %d\n", p_cfg->asOutputConfig[2].ui8Enabled);
8151  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32MaxPktSize = %u\n", p_cfg->ui32MaxPktSize);
8152  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
8153  {
8154  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8Force8Bit %d\n", i, p_cfg->asOutputConfig[i].ui8Force8Bit);
8155  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8SemiPlanarEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8SemiPlanarEnabled);
8156  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8CropMode %d\n", i, p_cfg->asOutputConfig[i].ui8CropMode);
8157  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sCroppingRectable.ui16XYWH %d,%d - %d x %d\n", i,
8162  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] sOutputPictureSize.ui16Width x height %d x %d\n", i,
8165  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleEnabled %d\n", i, p_cfg->asOutputConfig[i].ui8ScaleEnabled);
8166  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleAdapt %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt);
8167  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8EnablePpuScaleLimit %u\n", i, p_cfg->asOutputConfig[i].ui8EnablePpuScaleLimit);
8168  ni_log2(p_ctx, NI_LOG_DEBUG, "[%d] ui8ScaleResCeil %u\n", i, p_cfg->asOutputConfig[i].ui8ScaleResCeil);
8169  }
8170  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8MaxExtraHwFrameCnt %u\n", p_cfg->ui8MaxExtraHwFrameCnt);
8171  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EcPolicy = %u\n", p_cfg->ui8EcPolicy);
8172  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAdvancedEc = %u\n", p_cfg->ui8EnableAdvancedEc);
8173  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnablelowDelayCheck = %u\n", p_cfg->ui8EnablelowDelayCheck);
8174  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8DisableAdaptiveBuffers = %u\n", p_cfg->ui8DisableAdaptiveBuffers);
8175 
8176 }
8177 
8178 
8179 /*!******************************************************************************
8180  * \brief Setup all xcoder configurations with custom parameters (Rev. B)
8181  *
8182  * \param
8183  *
8184  * \return
8185  ******************************************************************************/
8187  ni_encoder_config_t *p_cfg,
8188  ni_xcoder_params_t *p_src)
8189 {
8190  ni_t408_config_t* p_t408 = NULL;
8191  ni_encoder_cfg_params_t *p_enc = NULL;
8192  int i = 0;
8193 
8194  if ((!p_ctx) || (!p_cfg) || (!p_src))
8195  {
8196  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
8197  __func__);
8198  return;
8199  }
8200  p_t408 = &(p_cfg->niParamT408);
8201  p_enc = &p_src->cfg_enc_params;
8202 
8203  ni_set_default_template(p_ctx, p_cfg);
8204 
8205  p_cfg->i32picWidth = p_src->source_width;
8206  p_cfg->i32picHeight = p_src->source_height;
8207  p_t408->tier = p_enc->high_tier;
8208  p_t408->gop_preset_index = p_enc->gop_preset_index;
8210  p_t408->cu_size_mode = p_enc->cu_size_mode;
8211  p_t408->max_num_merge = p_enc->max_num_merge;
8212  p_cfg->ui8AiEnhanceMode = p_src->enable_ai_enhance;
8213  p_cfg->ui8enable2PassGopPatern = p_src->enable2PassGop;
8214  // enhance_level is in range [1,3] set level when paramters is valid, otherwise make it to 1.
8215  if(p_cfg->ui8AiEnhanceMode && p_src->ai_enhance_level > 0 && p_src->ai_enhance_level < 4){
8216  p_cfg->ui8AiEnhanceLevel = p_src->ai_enhance_level;
8217  }else{
8218  p_cfg->ui8AiEnhanceLevel = 1;
8219  }
8221  p_cfg->i8skipFrameEnable = p_enc->skip_frame_enable;
8223  p_cfg->u8skipFrameInterval = p_enc->skip_frame_interval;
8224  p_cfg->ui16iFrameSizeRatio = p_enc->iframe_size_ratio;
8225  p_cfg->ui8EnableAcqLimit = p_enc->enable_acq_limit;
8226 
8227  // enable_dynamic_8x8_merge, enable_dynamic_16x16_merge, enable_dynamic_32x32_merge,
8228  // trans_rate, enable_hvs_qp_scale:
8229  // are not present in Rev B p_config
8230 
8231  p_cfg->ui8rcEnable = p_enc->rc.enable_rate_control;
8232 
8233  if(p_ctx->last_bitrate != 0)
8234  {
8235  // Slow sequence change happened. Retain the last bitrate.
8236  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_bitrate %d. assigned bitrate %d\n",
8237  __FUNCTION__, p_ctx->last_bitrate, p_src->bitrate);
8238  p_src->bitrate = p_ctx->last_bitrate;
8239  }
8240 
8241  if (p_src->bitrate != 0)
8242  {
8243  p_cfg->i32bitRate = p_src->bitrate;
8244  }
8245 
8246  // Update the bitrate to be used after Slow sequence change
8247  p_ctx->last_bitrate = p_cfg->i32bitRate;
8248 
8249 #if 0
8250  if ((p_enc->rc.enable_rate_control == 0) &&
8251  ((p_enc->rc.enable_mb_level_rc == 1) || (p_enc->rc.enable_cu_level_rate_control == 1)))
8252  {
8254  ni_log2(p_ctx, NI_LOG_DEBUG, "force enable_mb_level_rc & enable_cu_level_rate_control to 0 because rate control is disabled\n");
8255  }
8256 #endif
8257 
8259  p_t408->enable_hvs_qp = p_enc->rc.enable_hvs_qp;
8260  p_t408->hvs_qp_scale = p_enc->rc.hvs_qp_scale;
8261  p_t408->minQpI = p_enc->rc.min_qp;
8262  p_t408->minQpP = p_enc->rc.min_qp;
8263  p_t408->minQpB = p_enc->rc.min_qp;
8264  p_t408->maxQpI = p_enc->rc.max_qp;
8265  p_t408->maxQpP = p_enc->rc.max_qp;
8266  p_t408->maxQpB = p_enc->rc.max_qp;
8267 
8268  p_t408->max_delta_qp = p_enc->rc.max_delta_qp;
8269  if (p_enc->rc.vbv_buffer_size != -1)
8270  {
8271  p_cfg->i32vbvBufferSize = p_enc->rc.vbv_buffer_size;
8272  }
8273  else
8274  {
8275  if (p_enc->rc.enable_rate_control)
8276  p_cfg->i32vbvBufferSize = 3000; // enable CBR (default vbv buffer size 3000) even if user does not set vbvBufferSize
8277  else
8278  p_cfg->i32vbvBufferSize = 0; // if user sets CRF but not vbvBufferSize, do not eanble capped CRF
8279  }
8280  p_cfg->ui32vbvMaxRate = p_enc->rc.vbv_max_rate;
8281  p_cfg->i8intraQpDelta = p_enc->rc.intra_qp_delta;
8282  p_cfg->ui8fillerEnable = p_enc->rc.enable_filler;
8283  p_cfg->ui8picSkipEnable = p_enc->rc.enable_pic_skip;
8284  p_cfg->ui16maxFrameSize = p_enc->maxFrameSize / 2000;
8285  p_t408->intra_period = p_enc->intra_period;
8286  p_t408->roiEnable = p_enc->roi_enable;
8287  p_t408->useLongTerm = p_enc->long_term_ref_enable;
8288  if (QUADRA)
8289  {
8291  p_cfg->ui8setLongTermCount = p_enc->long_term_ref_count;
8292  }
8293  p_t408->conf_win_top = p_enc->conf_win_top;
8294  p_t408->conf_win_bottom = p_enc->conf_win_bottom;
8295  p_t408->conf_win_left = p_enc->conf_win_left;
8296  p_t408->conf_win_right = p_enc->conf_win_right;
8297  p_t408->avcIdrPeriod = p_enc->intra_period;
8298 
8299  if (QUADRA)
8300  {
8301  if(p_ctx->last_framerate.framerate_num != 0)
8302  {
8303  // Slow sequence change happened. Retain the last framerate.
8304  ni_log2(p_ctx, NI_LOG_DEBUG, "### %s: Slow sequence happened retain last_framerate num %d den %d. assigned num %d den %d\n",
8305  __FUNCTION__, p_ctx->last_framerate.framerate_num, p_ctx->last_framerate.framerate_denom,
8306  p_cfg->i32frameRateInfo, p_cfg->i32frameRateDenominator);
8307  p_src->fps_number = p_ctx->last_framerate.framerate_num;
8309 
8310  if (!p_src->enable_vfr) {
8311  p_enc->frame_rate = (int)(p_src->fps_number / p_src->fps_denominator);
8312  }
8313  }
8314 
8315  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8316  {
8317  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8318  p_cfg->i32frameRateDenominator = 1;
8319  if (p_src->fps_denominator != 0 &&
8320  (p_src->fps_number % p_src->fps_denominator) != 0)
8321  {
8322  uint32_t numUnitsInTick = 1000;
8323  p_cfg->i32frameRateDenominator = numUnitsInTick + 1;
8324  p_cfg->i32frameRateInfo += 1;
8325  p_cfg->i32frameRateInfo *= numUnitsInTick;
8326  }
8327  }
8328 
8329  // Update the framerate to be used after Slow sequence change
8332  }
8333  else
8334  {
8335  if (p_cfg->i32frameRateInfo != p_enc->frame_rate)
8336  {
8337  p_cfg->i32frameRateInfo = p_enc->frame_rate;
8338  p_t408->numUnitsInTick = 1000;
8339  if (p_src->fps_denominator != 0 &&
8340  (p_src->fps_number % p_src->fps_denominator) != 0)
8341  {
8342  p_t408->numUnitsInTick += 1;
8343  p_cfg->i32frameRateInfo += 1;
8344  }
8345  p_t408->timeScale = p_cfg->i32frameRateInfo * 1000;
8346  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
8347  {
8348  p_t408->timeScale *= 2;
8349  }
8350  }
8351  }
8352 
8353  p_t408->intra_qp = p_enc->rc.intra_qp;
8354 
8355  // "repeatHeaders" value 1 (all I frames) maps to forcedHeaderEnable
8356  // value 2; all other values are ignored
8357  if (p_t408->forcedHeaderEnable != p_enc->forced_header_enable &&
8359  {
8360  p_t408->forcedHeaderEnable = 2;
8361  p_cfg->ui8repeatHeaders = p_enc->forced_header_enable;
8362  }
8363 
8365 
8366  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8367  {
8368  switch (p_t408->decoding_refresh_type)
8369  {
8370  case 0: // Non-IRAP I-p_frame
8371  {
8372  // intra_period set to user-configured (above), avcIdrPeriod set to 0
8373  p_t408->avcIdrPeriod = 0;
8374  break;
8375  }
8376  case 1: // CRA
8377  case 2: // IDR
8378  {
8379  // intra_period set to 0, avcIdrPeriod set to user-configured (above)
8380  p_t408->intra_period = 0;
8381  break;
8382  }
8383  default:
8384  {
8385  ni_log(
8386  NI_LOG_ERROR,
8387  "ERROR: %s() unknown value for p_t408->decoding_refresh_type: %d\n",
8388  __func__, p_t408->decoding_refresh_type);
8389  break;
8390  }
8391  }
8392  } else if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
8393  STD_AV1 == p_cfg->ui8bitstreamFormat)
8394  {
8395  p_t408->avcIdrPeriod = 0;
8396  }
8397 
8398  // Rev. B: H.264 only parameters.
8399  p_t408->enable_transform_8x8 = p_enc->enable_transform_8x8;
8400  p_t408->entropy_coding_mode = p_enc->entropy_coding_mode;
8401 
8402  // Rev. B: shared between HEVC and H.264
8403  p_t408->slice_mode = p_enc->slice_mode;
8404  p_t408->slice_arg = p_enc->slice_arg;
8405  if (p_t408->intra_mb_refresh_mode != p_enc->intra_mb_refresh_mode)
8406  {
8408  if (1 != p_t408->intra_mb_refresh_mode)
8409  {
8410  p_t408->intra_mb_refresh_mode = 1;
8411  ni_log2(p_ctx, NI_LOG_DEBUG, "force intraRefreshMode to 1 because quadra only supports intra refresh by rows\n");
8412  }
8413  }
8414 
8415  if (p_t408->intra_mb_refresh_arg != p_enc->intra_mb_refresh_arg)
8416  {
8417  p_t408->intra_mb_refresh_arg = p_enc->intra_mb_refresh_arg;
8418  if (1 == p_t408->intra_mb_refresh_mode)
8419  {
8420  int mbHeight = (p_cfg->ui8bitstreamFormat == STD_AVC) ? 16 : 64;
8421  int mbRows = (p_cfg->i32picHeight + mbHeight - 1) / mbHeight;
8422  p_cfg->ui16gdrDuration = (mbRows + p_t408->intra_mb_refresh_arg - 1) / p_t408->intra_mb_refresh_arg;
8423  }
8424  }
8425 
8426  p_cfg->ui8intraResetRefresh = p_enc->intra_reset_refresh;
8427 
8428  // Rev. B: could be shared for HEVC and H.264
8429  p_t408->enable_mb_level_rc = p_enc->rc.enable_mb_level_rc;
8430 
8431  // profile setting: if user specified profile
8432  if (0 != p_enc->profile)
8433  {
8434  p_t408->profile = p_enc->profile;
8435  }
8436 
8437  p_t408->level = p_enc->level_idc;
8438 
8439  // main, extended or baseline profile of 8 bit (if input is 10 bit, Quadra auto converts to 8 bit) H.264 requires the following:
8440  // main: profile = 2 transform8x8Enable = 0
8441  // extended: profile = 3 entropyCodingMode = 0, transform8x8Enable = 0
8442  // baseline: profile = 1 entropyCodingMode = 0, transform8x8Enable = 0 and
8443  // gop with no B frames (gopPresetIdx=1, 2, 6, or 0
8444  // (custom with no B frames)
8445  if (STD_AVC == p_cfg->ui8bitstreamFormat)
8446  {
8447  if (2 == p_t408->profile)
8448  {
8449  p_t408->enable_transform_8x8 = 0;
8450  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8 set to 0 for profile 2 (main)\n");
8451  }
8452  else if (3 == p_t408->profile || 1 == p_t408->profile)
8453  {
8454  p_t408->entropy_coding_mode = p_t408->enable_transform_8x8 = 0;
8455  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode and enable_transform_8x8 set to 0 "
8456  "for profile 3 (extended) or 1 (baseline)\n");
8457  }
8458  }
8459 
8460  if (QUADRA)
8461  {
8462  if (0 == p_t408->entropy_coding_mode && 1 == p_enc->EnableRdoQuant)
8463  {
8464  ni_log2(p_ctx, NI_LOG_DEBUG, "RDOQ does not support entropy_coding_mode 0 (CAVLC) "
8465  "force EnableRdoQuant 0 to accommodate HW limiation\n");
8466  p_enc->EnableRdoQuant = 0;
8467  }
8468  }
8469 
8470 #ifndef QUADRA
8471  if (!QUADRA)
8472  {
8473  if (GOP_PRESET_IDX_CUSTOM == p_t408->gop_preset_index)
8474  {
8476  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
8477  {
8480  p_t408->custom_gop_params.pic_param[i].pic_qp = p_enc->custom_gop_params.pic_param[i].pic_qp + p_t408->intra_qp;
8481  p_t408->custom_gop_params.pic_param[i].num_ref_pic_L0 = p_enc->custom_gop_params.pic_param[i].num_ref_pic_L0;
8482  p_t408->custom_gop_params.pic_param[i].ref_poc_L0 = p_enc->custom_gop_params.pic_param[i].ref_poc_L0;
8483  p_t408->custom_gop_params.pic_param[i].ref_poc_L1 = p_enc->custom_gop_params.pic_param[i].ref_poc_L1;
8485  }
8486  }
8487  }
8488  else // QUADRA
8489 #endif
8490  {
8491  if (p_enc->custom_gop_params.custom_gop_size &&
8494  {
8497  for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
8498  {
8507  p_t408->custom_gop_params.pic_param[i].pic_type =
8511  for (int j = 0;
8512  j < p_enc->custom_gop_params.pic_param[i].num_ref_pics; j++)
8513  {
8514  p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic =
8515  p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic;
8518  }
8519  }
8520  }
8521  }
8522 
8523  p_ctx->key_frame_type = p_t408->decoding_refresh_type; //Store to use when force key p_frame
8524 
8525  // forceFrameType=1 requires intraPeriod=0 and avcIdrPeriod=0 and gopPresetIdx=8
8526  if (1 == p_src->force_frame_type)
8527  {
8528  p_t408->intra_period = 0;
8529  p_t408->avcIdrPeriod = 0;
8530  p_t408->gop_preset_index = 8;
8531  p_ctx->force_frame_type = 1;
8532  }
8533 
8534  p_cfg->hdrEnableVUI = p_src->hdrEnableVUI;
8535 
8536  if (p_cfg->i32hwframes != p_src->hwframes)
8537  {
8538  if (p_src->hwframes && p_ctx->auto_dl_handle == 0)
8539  {
8540  p_cfg->i32hwframes = p_src->hwframes;
8541  }
8542  else
8543  {
8544  p_cfg->i32hwframes = 0;
8545  }
8546  }
8547  p_cfg->ui16rootBufId = p_src->rootBufId;
8548 
8549  //set VUI info deprecated
8550  //p_cfg->ui32VuiDataSizeBits = p_src->ui32VuiDataSizeBits;
8551  //p_cfg->ui32VuiDataSizeBytes = p_src->ui32VuiDataSizeBytes;
8552  //memcpy(p_cfg->ui8VuiRbsp, p_src->ui8VuiRbsp, NI_MAX_VUI_SIZE);
8553  //if ((p_src->pos_num_units_in_tick > p_src->ui32VuiDataSizeBits) || (p_src->pos_time_scale > p_src->ui32VuiDataSizeBits))
8554  //{
8555  // ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() VUI filling error\n", __func__);
8556  // return;
8557  //}
8558  //else
8559  //{
8560  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_num_units_in_tick, p_t408->numUnitsInTick);
8561  // ni_fix_VUI(p_cfg->ui8VuiRbsp, p_src->pos_time_scale, p_t408->timeScale);
8562  //}
8563 
8564  if (p_src->enable_vfr)
8565  {
8566  p_cfg->ui8fixedframerate = 0;
8567  } else
8568  {
8569  p_cfg->ui8fixedframerate = 1;
8570  }
8571 
8572  //new QUADRA param
8573  if (p_enc->EnableAUD != 0)
8574  {
8575  p_cfg->ui8EnableAUD = p_enc->EnableAUD;
8576  }
8577  if (p_enc->lookAheadDepth != 0)
8578  {
8579  p_cfg->ui8LookAheadDepth = p_enc->lookAheadDepth;
8580  }
8581  if (p_enc->rdoLevel != 1)
8582  {
8583  p_cfg->ui8rdoLevel = p_enc->rdoLevel;
8584  }
8585  if (p_enc->crf != -1)
8586  {
8587  p_cfg->i8crf = p_enc->crf;
8588  }
8589  if (p_enc->HDR10MaxLight != 0)
8590  {
8591  p_cfg->ui16HDR10MaxLight = p_enc->HDR10MaxLight;
8592  }
8593  if (p_enc->HDR10AveLight != 0)
8594  {
8595  p_cfg->ui16HDR10AveLight = p_enc->HDR10AveLight;
8596  }
8597  if (p_enc->HDR10CLLEnable != 0)
8598  {
8599  p_cfg->ui8HDR10CLLEnable = p_enc->HDR10CLLEnable;
8600  }
8601  if (p_enc->HDR10Enable != 0)
8602  {
8603  p_cfg->ui8hdr10_enable = p_enc->HDR10Enable;
8604  p_cfg->ui16hdr10_dx0 = p_enc->HDR10dx0;
8605  p_cfg->ui16hdr10_dy0 = p_enc->HDR10dy0;
8606  p_cfg->ui16hdr10_dx1 = p_enc->HDR10dx1;
8607  p_cfg->ui16hdr10_dy1 = p_enc->HDR10dy1;
8608  p_cfg->ui16hdr10_dx2 = p_enc->HDR10dx2;
8609  p_cfg->ui16hdr10_dy2 = p_enc->HDR10dy2;
8610  p_cfg->ui16hdr10_wx = p_enc->HDR10wx;
8611  p_cfg->ui16hdr10_wy = p_enc->HDR10wy;
8612  p_cfg->ui32hdr10_maxluma = p_enc->HDR10maxluma;
8613  p_cfg->ui32hdr10_minluma = p_enc->HDR10minluma;
8614  }
8615 
8616  if (p_enc->EnableRdoQuant != 0)
8617  {
8618  p_cfg->ui8EnableRdoQuant = p_enc->EnableRdoQuant;
8619  }
8620  if (p_enc->ctbRcMode != 0)
8621  {
8622  p_cfg->ui8ctbRcMode = p_enc->ctbRcMode;
8623  }
8624  if (p_enc->gopSize != 0)
8625  {
8626  p_cfg->ui8gopSize = p_enc->gopSize;
8627  }
8628  if (p_src->use_low_delay_poc_type != 0)
8629  {
8631  }
8632  if (p_enc->gopLowdelay != 0)
8633  {
8634  p_cfg->ui8gopLowdelay = p_enc->gopLowdelay;
8635  }
8636  if (p_enc->gdrDuration != 0)
8637  {
8638  p_cfg->ui16gdrDuration = p_enc->gdrDuration;
8639  }
8640  if (p_enc->colorDescPresent)
8641  {
8642  p_cfg->ui8colorDescPresent = 1;
8643  p_cfg->ui8colorPrimaries = p_enc->colorPrimaries;
8644  p_cfg->ui8colorTrc = p_enc->colorTrc;
8645  p_cfg->ui8colorSpace = p_enc->colorSpace;
8646  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
8647  }
8648  if (p_enc->videoFullRange)
8649  {
8650  p_cfg->ui8videoFullRange = p_enc->videoFullRange;
8651  }
8652  if (p_enc->hrdEnable != 0)
8653  {
8654  p_cfg->ui8hrdEnable = p_enc->hrdEnable;
8655  }
8656 
8657  p_cfg->ui8planarFormat = p_src->cfg_enc_params.planar;
8658  p_cfg->ui16aspectRatioWidth = p_enc->aspectRatioWidth;
8659  p_cfg->ui16aspectRatioHeight = p_enc->aspectRatioHeight;
8660 
8661  if (p_enc->ltrRefInterval != 0)
8662  {
8663  p_cfg->ui32ltrRefInterval = p_enc->ltrRefInterval;
8664  p_cfg->i32ltrRefQpOffset = p_enc->ltrRefQpOffset;
8665  p_cfg->ui32ltrFirstGap = p_enc->ltrFirstGap;
8666  p_cfg->ui32ltrNextInterval = p_enc->ltrNextInterval;
8667  }
8668  if (p_enc->multicoreJointMode != 0)
8669  {
8670  p_cfg->ui8multicoreJointMode = p_enc->multicoreJointMode;
8671  }
8672  p_cfg->ui32QLevel = p_enc->qlevel;
8673 
8674  if (p_enc->chromaQpOffset != 0)
8675  {
8676  p_cfg->i8chromaQpOffset = p_enc->chromaQpOffset;
8677  }
8678 
8679  if (p_enc->tolCtbRcInter != (float)0.1)
8680  {
8681  p_cfg->i32tolCtbRcInter = (int32_t)(p_enc->tolCtbRcInter * 1000);
8682  }
8683 
8684  if (p_enc->tolCtbRcIntra != (float)0.1)
8685  {
8686  p_cfg->i32tolCtbRcIntra = (int32_t)(p_enc->tolCtbRcIntra * 1000);
8687  }
8688 
8689  if (p_enc->bitrateWindow != -255)
8690  {
8691  p_cfg->i16bitrateWindow = p_enc->bitrateWindow;
8692  }
8693 
8694  if (p_enc->inLoopDSRatio != 1)
8695  {
8696  p_cfg->ui8inLoopDSRatio = p_enc->inLoopDSRatio;
8697  }
8698 
8699  if (p_enc->blockRCSize != 0)
8700  {
8701  p_cfg->ui8blockRCSize = p_enc->blockRCSize;
8702  }
8703 
8704  if (p_enc->rcQpDeltaRange != 10)
8705  {
8706  p_cfg->ui8rcQpDeltaRange = p_enc->rcQpDeltaRange;
8707  }
8708 
8709  if (p_enc->ctbRowQpStep != 0)
8710  {
8711  p_cfg->i16ctbRowQpStep = p_enc->ctbRowQpStep;
8712  }
8713 
8714  if (p_enc->newRcEnable != -1)
8715  {
8716  p_cfg->ui8NewRCEnable = p_enc->newRcEnable;
8717  }
8718 
8719  // convert enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp to ctbRcMode
8720  if (QUADRA)
8721  {
8722  // ctbRcMode has priority over enable_mb_level_rc, enable_cu_level_rate_control, and enable_hvs_qp
8723  if (!p_cfg->ui8ctbRcMode)
8724  {
8725  if (p_t408->enable_mb_level_rc || p_t408->enable_cu_level_rate_control)
8726  {
8727  if (p_t408->enable_hvs_qp)
8728  {
8729  p_cfg->ui8ctbRcMode = 3;
8730  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
8731  p_t408->strongIntraSmoothEnable = 0;
8733  "Turning off strongIntraSmoothing because hvsQPEnable=1 "
8734  "for better subjective VQ\n");
8735  } else
8736  {
8737  p_cfg->ui8ctbRcMode = 2;
8738  }
8739  }
8740  else if (p_t408->enable_hvs_qp)
8741  {
8742  p_cfg->ui8ctbRcMode = 1;
8743  // If hvsQP is enabled, disable strongIntraSmooth to apply one filtering at a time for better VQ.
8744  p_t408->strongIntraSmoothEnable = 0;
8746  "Turning off strongIntraSmoothing because hvsQPEnable=1 for "
8747  "better subjective VQ\n");
8748  }
8749  }
8750  }
8751 
8752  if (p_src->low_delay_mode != 0)
8753  {
8754  p_cfg->ui8LowDelay = !!(p_src->low_delay_mode);
8755  }
8756 
8757  if (p_enc->enable_ssim != 0)
8758  {
8759  p_cfg->ui8enableSSIM = p_enc->enable_ssim;
8760  }
8761 
8762  if (p_enc->av1_error_resilient_mode != 0)
8763  {
8765  }
8766 
8767  if (p_enc->temporal_layers_enable != 0)
8768  {
8770  }
8771 
8772  if (p_ctx->pixel_format != NI_PIX_FMT_YUV420P)
8773  {
8774  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
8775  }
8776 
8777  if (p_enc->get_psnr_mode != 3)
8778  {
8779  p_cfg->ui8compressor = p_enc->get_psnr_mode;
8780  if (p_cfg->ui8PixelFormat != NI_PIX_FMT_YUV420P)
8781  {
8782  p_cfg->ui8compressor = 3;
8783  p_enc->get_psnr_mode = 3;
8784  ni_log(NI_LOG_INFO, "Warning get psnr feature only support YUV420P now\n");
8785  }
8786  }
8787 
8788  if (p_src->zerocopy_mode == -1) // zero copy auto mode - disable zero copy for low resolution
8789  {
8790  bool is_rgba = (p_ctx->pixel_format == NI_PIX_FMT_RGBA ||
8791  p_ctx->pixel_format == NI_PIX_FMT_BGRA ||
8792  p_ctx->pixel_format == NI_PIX_FMT_ARGB ||
8793  p_ctx->pixel_format == NI_PIX_FMT_ABGR) ? true : false;
8794  if (is_rgba ||
8796  "6Q") >= 0) &&
8798  p_src->zerocopy_mode = 1;
8799  else
8800  p_src->zerocopy_mode = 0;
8801  }
8802 
8803  if (p_src->zerocopy_mode)
8804  {
8805  p_cfg->ui32lumaLinesize = p_src->luma_linesize;
8806  p_cfg->ui32chromaLinesize = p_src->chroma_linesize;
8807  }
8808  else
8809  {
8810  p_cfg->ui32lumaLinesize = p_src->luma_linesize = 0;
8811  p_cfg->ui32chromaLinesize = p_src->chroma_linesize = 0;
8812  }
8813  // for fast sequence change linesize check
8814  p_ctx->ori_luma_linesize = p_src->luma_linesize;
8815  p_ctx->ori_chroma_linesize = p_src->chroma_linesize;
8816 
8817  // calculate number for frames delay for minFramesDelay
8818  int lookAheadEnable = !!p_cfg->ui8LookAheadDepth;
8819  int gopSize = g_map_preset_to_gopsize[lookAheadEnable][p_t408->gop_preset_index + 1];
8820  int mulitcoreDelay = p_cfg->ui8multicoreJointMode ? 3 : 0;
8821 
8822  if (p_t408->gop_preset_index == 0) // Custom GOP
8823  gopSize = p_t408->custom_gop_params.custom_gop_size;
8824 
8825  if (lookAheadEnable)
8826  {
8827  int firstGopEnd = gopSize + 1 + mulitcoreDelay; // first I-frame gopSize is 1
8828  int lookaheadGopEnd = mulitcoreDelay ?
8829  p_cfg->ui8LookAheadDepth + mulitcoreDelay + (gopSize - ((p_cfg->ui8LookAheadDepth-1+mulitcoreDelay) % gopSize)) :
8830  p_cfg->ui8LookAheadDepth + (gopSize - ((p_cfg->ui8LookAheadDepth-1) % gopSize)); // lookAheadDepth-1 because lookahead queue includes first I-frame
8831  int initialDelayNum = (firstGopEnd > lookaheadGopEnd) ? firstGopEnd : lookaheadGopEnd;
8832  int maxDelayNum = p_cfg->ui8LookAheadDepth + 1 + gopSize / 2 + mulitcoreDelay;
8833  int maxLookaheadQueue = initialDelayNum + (gopSize - 1) + mulitcoreDelay; // assume worst case scenario - gop size changes from initial gop to gop size 1
8834 
8835  p_ctx->initial_frame_delay = initialDelayNum + (mulitcoreDelay ? 4 : 0); // for multicore pass-2, need to add 4 more frames before pass-2 could output frame
8836  p_ctx->max_frame_delay = ((maxDelayNum > maxLookaheadQueue) ? maxDelayNum : maxLookaheadQueue) + (mulitcoreDelay ? 4 : 0); // for multicore pass-2, need to add 4 more frames before pass-2 could output frame
8838  "6r2") >= 0)
8839  {
8840  p_ctx->last_gop_size = gopSize; // for adaptive gop, gop size change can happen in pass-1, causing the first non-IDR output to carrry gop size 4 insetad of 8 and increase lookahead queue
8842  "6rX") >= 0)
8843  {
8844  if (p_t408->gop_preset_index == GOP_PRESET_IDX_DEFAULT || mulitcoreDelay) // for adaptive gop or multicore, just set max frame delay to workaround encoding stuck
8845  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
8846  else
8847  p_ctx->current_frame_delay = p_ctx->initial_frame_delay;
8848  }
8849  else
8850  {
8851  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
8852  }
8853  }
8854  else
8855  {
8856  p_ctx->last_gop_size = gopSize;
8857  p_ctx->current_frame_delay = p_ctx->max_frame_delay;
8858  }
8859  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: firstGopEnd %d lookaheadGopEnd %d initialDelayNum %d maxDelayNum %d maxLookaheadQueue %d\n",
8860  __FUNCTION__, firstGopEnd, lookaheadGopEnd, initialDelayNum, maxDelayNum, maxLookaheadQueue);
8861  }
8862  else
8863  {
8864  p_ctx->last_gop_size = gopSize;
8865  p_ctx->initial_frame_delay = p_ctx->max_frame_delay = p_ctx->current_frame_delay = gopSize + mulitcoreDelay;
8866  }
8867 
8868  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: preset %d lookAheadDepth %d gopSize %d mulitcoreDelay %d "
8869  "last_gop_size %d linitial_frame_delay %d current_frame_delay %d max_frame_delay %d\n",
8870  __FUNCTION__, p_t408->gop_preset_index, p_cfg->ui8LookAheadDepth, gopSize, mulitcoreDelay,
8871  p_ctx->last_gop_size, p_ctx->initial_frame_delay, p_ctx->current_frame_delay, p_ctx->max_frame_delay);
8872 
8873  if (p_enc->crop_width != 0 && p_enc->crop_height != 0)
8874  {
8875  p_cfg->ui32cropWidth = p_enc->crop_width;
8876  p_cfg->ui32cropHeight = p_enc->crop_height;
8877  p_cfg->ui32horOffset = p_enc->hor_offset;
8878  p_cfg->ui32verOffset = p_enc->ver_offset;
8879  }
8880 
8881  if (p_enc->crfMax != -1)
8882  {
8883  p_cfg->i8crfMax = (int8_t)(p_enc->crfMax) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
8884  }
8885 
8886  if (p_enc->qcomp != (float)0.6)
8887  {
8888  p_cfg->i32qcomp = (int32_t)(p_enc->qcomp * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
8889  }
8890 
8891  if (p_enc->noMbtree != 0)
8892  {
8893  p_cfg->ui8noMbtree = p_enc->noMbtree;
8894  }
8895 
8896  if (p_enc->noHWMultiPassSupport != 0)
8897  {
8899  }
8900 
8901  if (p_enc->cuTreeFactor != 5)
8902  {
8903  p_cfg->i8cuTreeFactor = p_enc->cuTreeFactor;
8904  }
8905 
8906  if (p_enc->ipRatio != (float)1.4)
8907  {
8908  p_cfg->i32ipRatio = (int32_t)(p_enc->ipRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
8909  }
8910 
8911  if (p_enc->pbRatio != (float)1.3)
8912  {
8913  p_cfg->i32pbRatio = (int32_t)(p_enc->pbRatio * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
8914  }
8915 
8916  if (p_enc->cplxDecay != (float)0.5)
8917  {
8918  p_cfg->i32cplxDecay = (int32_t)(p_enc->cplxDecay * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
8919  }
8920 
8921  if (p_enc->pps_init_qp != -1)
8922  {
8923  p_cfg->i8ppsInitQp = (int8_t)(p_enc->pps_init_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
8924  }
8925 
8926  if (p_enc->bitrateMode != -1)
8927  {
8928  p_cfg->ui8bitrateMode = p_enc->bitrateMode;
8929  }
8930 
8931  if (p_enc->pass1_qp != -1)
8932  {
8933  p_cfg->i8pass1Qp = (int8_t)(p_enc->pass1_qp) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
8934  }
8935 
8936  if (p_enc->crfFloat != (float)-1.0)
8937  {
8938  // for old libxcoder backward compatibility
8939  p_cfg->i8crf = (int8_t)(p_enc->crfFloat);
8940  p_cfg->i8crfDecimal = (int8_t)((p_enc->crfFloat - (float)p_cfg->i8crf) * 100);
8941  }
8942 
8943  if (p_enc->hvsBaseMbComplexity != 15)
8944  {
8945  p_cfg->i8hvsBaseMbComplexity = (int8_t)p_enc->hvsBaseMbComplexity - 15; // for old libxcoder backward compatibility, use -15 to represent 0
8946  }
8947 
8948  if (p_enc->enableipRatio != 0 && p_cfg->i32vbvBufferSize != 0) //vbvBufferSize !=0 for CBR not for ABR
8949  {
8950  p_cfg->i8enableipRatio = p_enc->enableipRatio;
8951  }
8952 
8953  if (p_enc->crf_max_iframe_enable != 0)
8954  {
8956  }
8957 
8958  if (p_enc->vbv_min_rate != 0)
8959  {
8960  p_cfg->ui32vbvMinRate = p_enc->vbv_min_rate;
8961  }
8962 
8963  if (p_enc->disableBframeRdoq != 0)
8964  {
8965  p_cfg->ui8disableBframeRDOQ = p_enc->disableBframeRdoq;
8966  }
8967 
8968  if (p_enc->forceBframeQpfactor != (float)-1.0)
8969  {
8970  p_cfg->i32forceBframeQpFactor = (int32_t)(p_enc->forceBframeQpfactor * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
8971  }
8972 
8973  if (p_enc->tune_bframe_visual != 0)
8974  {
8975  p_cfg->ui8tuneBframeVisual = p_enc->tune_bframe_visual;
8976  }
8977 
8978  ni_log2(p_ctx, NI_LOG_DEBUG, "lowDelay=%d\n", p_src->low_delay_mode);
8979  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitstreamFormat=%d\n", p_cfg->ui8bitstreamFormat);
8980  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picWidth=%d\n", p_cfg->i32picWidth);
8981  ni_log2(p_ctx, NI_LOG_DEBUG, "i32picHeight=%d\n", p_cfg->i32picHeight);
8982  ni_log2(p_ctx, NI_LOG_DEBUG, "i32meBlkMode=%d\n", p_cfg->i32meBlkMode);
8983  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8sliceMode=%d\n", p_cfg->ui8sliceMode);
8984  ni_log2(p_ctx, NI_LOG_DEBUG, "i32frameRateInfo=%d\n", p_cfg->i32frameRateInfo);
8985  ni_log2(p_ctx, NI_LOG_DEBUG, "i32vbvBufferSize=%d\n", p_cfg->i32vbvBufferSize);
8986  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMax=%d\n", p_cfg->i32userQpMax);
8987  ni_log2(p_ctx, NI_LOG_DEBUG, "enableSSIM=%d\n", p_cfg->ui8enableSSIM);
8988  // AVC only
8989  ni_log2(p_ctx, NI_LOG_DEBUG, "i32maxIntraSize=%d\n", p_cfg->i32maxIntraSize);
8990  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMaxDeltaQp=%d\n", p_cfg->i32userMaxDeltaQp);
8991  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userMinDeltaQp=%d\n", p_cfg->i32userMinDeltaQp);
8992  ni_log2(p_ctx, NI_LOG_DEBUG, "i32userQpMin=%d\n", p_cfg->i32userQpMin);
8993  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRate=%d\n", p_cfg->i32bitRate);
8994  ni_log2(p_ctx, NI_LOG_DEBUG, "i32bitRateBL=%d\n", p_cfg->i32bitRateBL);
8995  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcEnable=%d\n", p_cfg->ui8rcEnable);
8996  ni_log2(p_ctx, NI_LOG_DEBUG, "i32srcBitDepth=%d\n", p_cfg->i32srcBitDepth);
8997  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enablePTS=%d\n", p_cfg->ui8enablePTS);
8998  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8lowLatencyMode=%d\n", p_cfg->ui8lowLatencyMode);
8999  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32sourceEndian=%u\n", p_cfg->ui32sourceEndian);
9000  ni_log2(p_ctx, NI_LOG_DEBUG, "hdrEnableVUI=%u\n", p_cfg->hdrEnableVUI);
9001  ni_log2(p_ctx, NI_LOG_DEBUG, "i32hwframes=%i\n", p_cfg->i32hwframes);
9002 
9003  ni_log2(p_ctx, NI_LOG_DEBUG, "** ni_t408_config_t: \n");
9004  ni_log2(p_ctx, NI_LOG_DEBUG, "profile=%d\n", p_t408->profile);
9005  ni_log2(p_ctx, NI_LOG_DEBUG, "level=%d\n", p_t408->level);
9006  ni_log2(p_ctx, NI_LOG_DEBUG, "tier=%d\n", p_t408->tier);
9007 
9008  ni_log2(p_ctx, NI_LOG_DEBUG, "internalBitDepth=%d\n", p_t408->internalBitDepth);
9009  ni_log2(p_ctx, NI_LOG_DEBUG, "losslessEnable=%d\n", p_t408->losslessEnable);
9010  ni_log2(p_ctx, NI_LOG_DEBUG, "constIntraPredFlag=%d\n", p_t408->constIntraPredFlag);
9011 
9012  ni_log2(p_ctx, NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_t408->decoding_refresh_type);
9013  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_qp=%d\n", p_t408->intra_qp);
9014  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_period=%d\n", p_t408->intra_period);
9015  ni_log2(p_ctx, NI_LOG_DEBUG, "roi_enable=%d\n", p_t408->roiEnable);
9016 
9017  ni_log2(p_ctx, NI_LOG_DEBUG, "useLongTerm=%u\n", p_t408->useLongTerm);
9018  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermInterval=%u\n", p_cfg->ui32setLongTermInterval);
9019  ni_log2(p_ctx, NI_LOG_DEBUG, "setLongTermCount=%u\n", p_cfg->ui8setLongTermCount);
9020 
9021  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_top=%d\n", p_t408->conf_win_top);
9022  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_t408->conf_win_bottom);
9023  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_left=%d\n", p_t408->conf_win_left);
9024  ni_log2(p_ctx, NI_LOG_DEBUG, "conf_win_right=%d\n", p_t408->conf_win_right);
9025 
9026  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceMode=%d\n", p_t408->independSliceMode);
9027  ni_log2(p_ctx, NI_LOG_DEBUG, "independSliceModeArg=%d\n", p_t408->independSliceModeArg);
9028 
9029  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceMode=%d\n", p_t408->dependSliceMode);
9030  ni_log2(p_ctx, NI_LOG_DEBUG, "dependSliceModeArg=%d\n", p_t408->dependSliceModeArg);
9031 
9032  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshMode=%d\n", p_t408->intraRefreshMode);
9033 
9034  ni_log2(p_ctx, NI_LOG_DEBUG, "intraRefreshArg=%d\n", p_t408->intraRefreshArg);
9035 
9036  ni_log2(p_ctx, NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_t408->use_recommend_enc_params);
9037  ni_log2(p_ctx, NI_LOG_DEBUG, "scalingListEnable=%d\n", p_t408->scalingListEnable);
9038 
9039  ni_log2(p_ctx, NI_LOG_DEBUG, "cu_size_mode=%d\n", p_t408->cu_size_mode);
9040  ni_log2(p_ctx, NI_LOG_DEBUG, "tmvpEnable=%d\n", p_t408->tmvpEnable);
9041  ni_log2(p_ctx, NI_LOG_DEBUG, "wppEnable=%d\n", p_t408->wppEnable);
9042  ni_log2(p_ctx, NI_LOG_DEBUG, "max_num_merge=%d\n", p_t408->max_num_merge);
9043  ni_log2(p_ctx, NI_LOG_DEBUG, "disableDeblk=%d\n", p_t408->disableDeblk);
9044  ni_log2(p_ctx, NI_LOG_DEBUG, "lfCrossSliceBoundaryEnable=%d\n", p_t408->lfCrossSliceBoundaryEnable);
9045  ni_log2(p_ctx, NI_LOG_DEBUG, "betaOffsetDiv2=%d\n", p_t408->betaOffsetDiv2);
9046  ni_log2(p_ctx, NI_LOG_DEBUG, "tcOffsetDiv2=%d\n", p_t408->tcOffsetDiv2);
9047  ni_log2(p_ctx, NI_LOG_DEBUG, "skipIntraTrans=%d\n", p_t408->skipIntraTrans);
9048  ni_log2(p_ctx, NI_LOG_DEBUG, "saoEnable=%d\n", p_t408->saoEnable);
9049  ni_log2(p_ctx, NI_LOG_DEBUG, "intraNxNEnable=%d\n", p_t408->intraNxNEnable);
9050  ni_log2(p_ctx, NI_LOG_DEBUG, "bitAllocMode=%d\n", p_t408->bitAllocMode);
9051 
9052  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_t408->enable_cu_level_rate_control);
9053 
9054  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_t408->enable_hvs_qp);
9055 
9056  ni_log2(p_ctx, NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_t408->hvs_qp_scale);
9057 
9058  ni_log2(p_ctx, NI_LOG_DEBUG, "max_delta_qp=%d\n", p_t408->max_delta_qp);
9059 
9060  // CUSTOM_GOP
9061  ni_log2(p_ctx, NI_LOG_DEBUG, "gop_preset_index=%d\n", p_t408->gop_preset_index);
9062 #ifndef QUADRA
9063  if (!QUADRA)
9064  {
9065  if (p_t408->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
9066  {
9067  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9068  for (i = 0; i < 8; i++)
9069  //for (i = 0; i < p_t408->custom_gop_params.custom_gop_size; i++)
9070  {
9071  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_t408->custom_gop_params.pic_param[i].pic_type);
9072  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_t408->custom_gop_params.pic_param[i].poc_offset);
9073  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_qp=%d\n", i, p_t408->custom_gop_params.pic_param[i].pic_qp);
9074  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pic_L0=%d\n", i, p_t408->custom_gop_params.pic_param[i].num_ref_pic_L0);
9075  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L0=%d\n", i, p_t408->custom_gop_params.pic_param[i].ref_poc_L0);
9076  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L1=%d\n", i, p_t408->custom_gop_params.pic_param[i].ref_poc_L1);
9077  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_t408->custom_gop_params.pic_param[i].temporal_id);
9078  }
9079  }
9080  }
9081  else // QUADRA
9082 #endif
9083  {
9084  if (p_t408->custom_gop_params.custom_gop_size)
9085  {
9086  int j;
9087  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_t408->custom_gop_params.custom_gop_size);
9088  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9089  {
9090  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_t408->custom_gop_params.pic_param[i].poc_offset);
9091  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_offset=%d\n", i, p_t408->custom_gop_params.pic_param[i].qp_offset);
9092  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_factor=%f\n", i, p_t408->custom_gop_params.pic_param[i].qp_factor);
9093  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_t408->custom_gop_params.pic_param[i].temporal_id);
9094  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_t408->custom_gop_params.pic_param[i].pic_type);
9095  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pics=%d\n", i, p_t408->custom_gop_params.pic_param[i].num_ref_pics);
9096  for (j = 0; j < NI_MAX_REF_PIC; j++)
9097  {
9098  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic=%d\n", i, j, p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic);
9099  ni_log2(p_ctx, NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic_used=%d\n", i, j, p_t408->custom_gop_params.pic_param[i].rps[j].ref_pic_used);
9100  }
9101  }
9102  }
9103  }
9104 
9105  ni_log2(p_ctx, NI_LOG_DEBUG, "roiEnable=%d\n", p_t408->roiEnable);
9106 
9107  ni_log2(p_ctx, NI_LOG_DEBUG, "numUnitsInTick=%u\n", p_t408->numUnitsInTick);
9108  ni_log2(p_ctx, NI_LOG_DEBUG, "timeScale=%u\n", p_t408->timeScale);
9109  ni_log2(p_ctx, NI_LOG_DEBUG, "numTicksPocDiffOne=%u\n", p_t408->numTicksPocDiffOne);
9110 
9111  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCbQpOffset=%d\n", p_t408->chromaCbQpOffset);
9112  ni_log2(p_ctx, NI_LOG_DEBUG, "chromaCrQpOffset=%d\n", p_t408->chromaCrQpOffset);
9113 
9114  ni_log2(p_ctx, NI_LOG_DEBUG, "initialRcQp=%d\n", p_t408->initialRcQp);
9115 
9116  ni_log2(p_ctx, NI_LOG_DEBUG, "nrYEnable=%u\n", p_t408->nrYEnable);
9117  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCbEnable=%u\n", p_t408->nrCbEnable);
9118  ni_log2(p_ctx, NI_LOG_DEBUG, "nrCrEnable=%u\n", p_t408->nrCrEnable);
9119 
9120  // ENC_NR_WEIGHT
9121  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightY=%u\n", p_t408->nrIntraWeightY);
9122  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCb=%u\n", p_t408->nrIntraWeightCb);
9123  ni_log2(p_ctx, NI_LOG_DEBUG, "nrIntraWeightCr=%u\n", p_t408->nrIntraWeightCr);
9124  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightY=%u\n", p_t408->nrInterWeightY);
9125  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCb=%u\n", p_t408->nrInterWeightCb);
9126  ni_log2(p_ctx, NI_LOG_DEBUG, "nrInterWeightCr=%u\n", p_t408->nrInterWeightCr);
9127 
9128  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseEstEnable=%u\n", p_t408->nrNoiseEstEnable);
9129  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaY=%u\n", p_t408->nrNoiseSigmaY);
9130  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCb=%u\n", p_t408->nrNoiseSigmaCb);
9131  ni_log2(p_ctx, NI_LOG_DEBUG, "nrNoiseSigmaCr=%u\n", p_t408->nrNoiseSigmaCr);
9132 
9133  // newly added for T408
9134  ni_log2(p_ctx, NI_LOG_DEBUG, "monochromeEnable=%u\n", p_t408->monochromeEnable);
9135  ni_log2(p_ctx, NI_LOG_DEBUG, "strongIntraSmoothEnable=%u\n",
9136  p_t408->strongIntraSmoothEnable);
9137 
9138  ni_log2(p_ctx, NI_LOG_DEBUG, "weightPredEnable=%u\n", p_t408->weightPredEnable);
9139  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDetectEnable=%u\n", p_t408->bgDetectEnable);
9140  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrDiff=%u\n", p_t408->bgThrDiff);
9141  ni_log2(p_ctx, NI_LOG_DEBUG, "bgThrMeanDiff=%u\n", p_t408->bgThrMeanDiff);
9142  ni_log2(p_ctx, NI_LOG_DEBUG, "bgLambdaQp=%u\n", p_t408->bgLambdaQp);
9143  ni_log2(p_ctx, NI_LOG_DEBUG, "bgDeltaQp=%d\n", p_t408->bgDeltaQp);
9144 
9145  ni_log2(p_ctx, NI_LOG_DEBUG, "customLambdaEnable=%u\n", p_t408->customLambdaEnable);
9146  ni_log2(p_ctx, NI_LOG_DEBUG, "customMDEnable=%u\n", p_t408->customMDEnable);
9147  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04DeltaRate=%d\n", p_t408->pu04DeltaRate);
9148  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08DeltaRate=%d\n", p_t408->pu08DeltaRate);
9149  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16DeltaRate=%d\n", p_t408->pu16DeltaRate);
9150  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32DeltaRate=%d\n", p_t408->pu32DeltaRate);
9151  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraPlanarDeltaRate=%d\n", p_t408->pu04IntraPlanarDeltaRate);
9152  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraDcDeltaRate=%d\n", p_t408->pu04IntraDcDeltaRate);
9153  ni_log2(p_ctx, NI_LOG_DEBUG, "pu04IntraAngleDeltaRate=%d\n", p_t408->pu04IntraAngleDeltaRate);
9154  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraPlanarDeltaRate=%d\n", p_t408->pu08IntraPlanarDeltaRate);
9155  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraDcDeltaRate=%d\n", p_t408->pu08IntraDcDeltaRate);
9156  ni_log2(p_ctx, NI_LOG_DEBUG, "pu08IntraAngleDeltaRate=%d\n", p_t408->pu08IntraAngleDeltaRate);
9157  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraPlanarDeltaRate=%d\n", p_t408->pu16IntraPlanarDeltaRate);
9158  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraDcDeltaRate=%d\n", p_t408->pu16IntraDcDeltaRate);
9159  ni_log2(p_ctx, NI_LOG_DEBUG, "pu16IntraAngleDeltaRate=%d\n", p_t408->pu16IntraAngleDeltaRate);
9160  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraPlanarDeltaRate=%d\n", p_t408->pu32IntraPlanarDeltaRate);
9161  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraDcDeltaRate=%d\n", p_t408->pu32IntraDcDeltaRate);
9162  ni_log2(p_ctx, NI_LOG_DEBUG, "pu32IntraAngleDeltaRate=%d\n", p_t408->pu32IntraAngleDeltaRate);
9163  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08IntraDeltaRate=%d\n", p_t408->cu08IntraDeltaRate);
9164  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08InterDeltaRate=%d\n", p_t408->cu08InterDeltaRate);
9165  ni_log2(p_ctx, NI_LOG_DEBUG, "cu08MergeDeltaRate=%d\n", p_t408->cu08MergeDeltaRate);
9166  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16IntraDeltaRate=%d\n", p_t408->cu16IntraDeltaRate);
9167  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16InterDeltaRate=%d\n", p_t408->cu16InterDeltaRate);
9168  ni_log2(p_ctx, NI_LOG_DEBUG, "cu16MergeDeltaRate=%d\n", p_t408->cu16MergeDeltaRate);
9169  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32IntraDeltaRate=%d\n", p_t408->cu32IntraDeltaRate);
9170  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32InterDeltaRate=%d\n", p_t408->cu32InterDeltaRate);
9171  ni_log2(p_ctx, NI_LOG_DEBUG, "cu32MergeDeltaRate=%d\n", p_t408->cu32MergeDeltaRate);
9172  ni_log2(p_ctx, NI_LOG_DEBUG, "coefClearDisable=%d\n", p_t408->coefClearDisable);
9173  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpI=%d\n", p_t408->minQpI);
9174  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpI=%d\n", p_t408->maxQpI);
9175  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpP=%d\n", p_t408->minQpP);
9176  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpP=%d\n", p_t408->maxQpP);
9177  ni_log2(p_ctx, NI_LOG_DEBUG, "minQpB=%d\n", p_t408->minQpB);
9178  ni_log2(p_ctx, NI_LOG_DEBUG, "maxQpB=%d\n", p_t408->maxQpB);
9179 
9180  // for H.264 on T408
9181  ni_log2(p_ctx, NI_LOG_DEBUG, "avcIdrPeriod=%d\n", p_t408->avcIdrPeriod);
9182  ni_log2(p_ctx, NI_LOG_DEBUG, "rdoSkip=%d\n", p_t408->rdoSkip);
9183  ni_log2(p_ctx, NI_LOG_DEBUG, "lambdaScalingEnable=%d\n", p_t408->lambdaScalingEnable);
9184  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_t408->enable_transform_8x8);
9185  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_mode=%d\n", p_t408->slice_mode);
9186  ni_log2(p_ctx, NI_LOG_DEBUG, "slice_arg=%d\n", p_t408->slice_arg);
9187  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_t408->intra_mb_refresh_mode);
9188  ni_log2(p_ctx, NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_t408->intra_mb_refresh_arg);
9189  ni_log2(p_ctx, NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_t408->enable_mb_level_rc);
9190  ni_log2(p_ctx, NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_t408->entropy_coding_mode);
9191  ni_log2(p_ctx, NI_LOG_DEBUG, "forcedHeaderEnable=%u\n", p_t408->forcedHeaderEnable);
9192 
9193  //QUADRA
9194  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAUD=%d\n", p_cfg->ui8EnableAUD);
9195  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LookAheadDepth=%d\n", p_cfg->ui8LookAheadDepth);
9196  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rdoLevel=%d\n", p_cfg->ui8rdoLevel);
9197  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crf=%d\n", p_cfg->i8crf);
9198  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9199  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10MaxLight=%d\n", p_cfg->ui16HDR10MaxLight);
9200  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16HDR10AveLight=%d\n", p_cfg->ui16HDR10AveLight);
9201  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8HDR10CLLEnable=%d\n", p_cfg->ui8HDR10CLLEnable);
9202  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableRdoQuant=%d\n", p_cfg->ui8EnableRdoQuant);
9203  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8ctbRcMode=%d\n", p_cfg->ui8ctbRcMode);
9204  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopSize=%d\n", p_cfg->ui8gopSize);
9205  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8useLowDelayPocType=%d\n", p_cfg->ui8useLowDelayPocType);
9206  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8gopLowdelay=%d\n", p_cfg->ui8gopLowdelay);
9207  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16gdrDuration=%d\n", p_cfg->ui16gdrDuration);
9208  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hrdEnable=%d\n", p_cfg->ui8hrdEnable);
9209  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorDescPresent=%d\n", p_cfg->ui8colorDescPresent);
9210  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorPrimaries=%d\n", p_cfg->ui8colorPrimaries);
9211  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorTrc=%d\n", p_cfg->ui8colorTrc);
9212  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8colorSpace=%d\n", p_cfg->ui8colorSpace);
9213  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioWidth=%d\n", p_cfg->ui16aspectRatioWidth);
9214  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16aspectRatioHeight=%d\n", p_cfg->ui16aspectRatioHeight);
9215  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16rootBufId=%d\n", p_cfg->ui16rootBufId);
9216  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8planarFormat=%d\n", p_cfg->ui8planarFormat);
9217  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8PixelFormat=%d\n", p_cfg->ui8PixelFormat);
9218  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrRefInterval=%u\n", p_cfg->ui32ltrRefInterval);
9219  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ltrRefQpOffset=%d\n", p_cfg->i32ltrRefQpOffset);
9220  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrFirstGap=%u\n", p_cfg->ui32ltrFirstGap);
9221  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32ltrNextInterval=%u\n", p_cfg->ui32ltrNextInterval);
9222  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8multicoreJointMode=%d\n", p_cfg->ui8multicoreJointMode);
9223  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8videoFullRange=%u\n", p_cfg->ui8videoFullRange);
9224  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32QLevel=%u\n", p_cfg->ui32QLevel);
9225  ni_log2(p_ctx, NI_LOG_DEBUG, "i8chromaQpOffset=%d\n", p_cfg->i8chromaQpOffset);
9226  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcInter=0x%x\n", p_cfg->i32tolCtbRcInter);
9227  ni_log2(p_ctx, NI_LOG_DEBUG, "i32tolCtbRcIntra=0x%x\n", p_cfg->i32tolCtbRcIntra);
9228  ni_log2(p_ctx, NI_LOG_DEBUG, "i16bitrateWindow=%d\n", p_cfg->i16bitrateWindow);
9229  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8inLoopDSRatio=%u\n", p_cfg->ui8inLoopDSRatio);
9230  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8blockRCSize=%u\n", p_cfg->ui8blockRCSize);
9231  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8rcQpDeltaRange=%u\n", p_cfg->ui8rcQpDeltaRange);
9232  ni_log2(p_ctx, NI_LOG_DEBUG, "i16ctbRowQpStep=%u\n", p_cfg->i16ctbRowQpStep);
9233  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8NewRCEnable=%u\n", p_cfg->ui8NewRCEnable);
9234  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8LowDelay=%d\n", p_cfg->ui8LowDelay);
9235  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8hdr10_enable=%u\n", p_cfg->ui8hdr10_enable);
9236  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx0=%u\n", p_cfg->ui16hdr10_dx0);
9237  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy0=%u\n", p_cfg->ui16hdr10_dy0);
9238  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx1=%u\n", p_cfg->ui16hdr10_dx1);
9239  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy1=%u\n", p_cfg->ui16hdr10_dy1);
9240  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dx2=%u\n", p_cfg->ui16hdr10_dx2);
9241  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_dy2=%u\n", p_cfg->ui16hdr10_dy2);
9242  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wx=%u\n", p_cfg->ui16hdr10_wx);
9243  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16hdr10_wy=%u\n", p_cfg->ui16hdr10_wy);
9244  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_maxluma=%u\n", p_cfg->ui32hdr10_maxluma);
9245  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32hdr10_minluma=%u\n", p_cfg->ui32hdr10_minluma);
9246  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8av1ErrResilientMode=%u\n", p_cfg->ui8av1ErrResilientMode);
9247  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8intraResetRefresh=%d\n", p_cfg->ui8intraResetRefresh);
9248  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8temporalLayersEnable=%u\n", p_cfg->ui8temporalLayersEnable);
9249  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8enable2PassGopPattern=%u\n", p_cfg->ui8enable2PassGopPatern);
9250  ni_log2(p_ctx, NI_LOG_DEBUG, "zerocopy_mode=%d\n", p_src->zerocopy_mode);
9251  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32lumaLinesize=%u\n", p_cfg->ui32lumaLinesize);
9252  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32chromaLinesize=%u\n", p_cfg->ui32chromaLinesize);
9253  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropWidth=%u\n", p_cfg->ui32cropWidth);
9254  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32cropHeight=%u\n", p_cfg->ui32cropHeight);
9255  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32horOffset=%u\n", p_cfg->ui32horOffset);
9256  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32verOffset=%u\n", p_cfg->ui32verOffset);
9257  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfMax=%d\n", p_cfg->i8crfMax);
9258  ni_log2(p_ctx, NI_LOG_DEBUG, "i32qcomp=%d\n", p_cfg->i32qcomp);
9259  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noMbtree=%u\n", p_cfg->ui8noMbtree);
9260  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8noHWMultiPassSupport=%u\n", p_cfg->ui8noHWMultiPassSupport);
9261  ni_log2(p_ctx, NI_LOG_DEBUG, "i8cuTreeFactor=%d\n", p_cfg->i8cuTreeFactor);
9262  ni_log2(p_ctx, NI_LOG_DEBUG, "i32ipRatio=%d\n", p_cfg->i32ipRatio);
9263  ni_log2(p_ctx, NI_LOG_DEBUG, "i32pbRatio=%d\n", p_cfg->i32pbRatio);
9264  ni_log2(p_ctx, NI_LOG_DEBUG, "i32cplxDecay=%d\n", p_cfg->i32cplxDecay);
9265  ni_log2(p_ctx, NI_LOG_DEBUG, "i8ppsInitQp=%d\n", p_cfg->i8ppsInitQp);
9266  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8bitrateMode=%u\n", p_cfg->ui8bitrateMode);
9267  ni_log2(p_ctx, NI_LOG_DEBUG, "i8pass1Qp=%d\n", p_cfg->i8pass1Qp);
9268  ni_log2(p_ctx, NI_LOG_DEBUG, "i8crfDecimal=%d\n", p_cfg->i8crfDecimal);
9269  ni_log2(p_ctx, NI_LOG_DEBUG, "i8hvsBaseMbComplexity=%u\n", p_cfg->i8hvsBaseMbComplexity);
9270  ni_log2(p_ctx, NI_LOG_DEBUG, "i8enableipRatio=%d\n",p_cfg->i8enableipRatio);
9271  ni_log2(p_ctx, NI_LOG_DEBUG, "ui16iFrameSizeRatio=%u\n",p_cfg->ui16iFrameSizeRatio);
9272  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8crfMaxIframeEnable=%u\n", p_cfg->ui8crfMaxIframeEnable);
9273  ni_log2(p_ctx, NI_LOG_DEBUG, "ui32vbvMinRate=%u\n", p_cfg->ui32vbvMinRate);
9274  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8disableBframeRDOQ=%u\n", p_cfg->ui8disableBframeRDOQ);
9275  ni_log2(p_ctx, NI_LOG_DEBUG, "i32forceBframeQpFactor=%d\n", p_cfg->i32forceBframeQpFactor);
9276  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8tuneBframeVisual=%u\n", p_cfg->ui8tuneBframeVisual);
9277  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8EnableAcqLimit=%u\n", p_cfg->ui8EnableAcqLimit);
9278  ni_log2(p_ctx, NI_LOG_DEBUG, "ui8compressor=%u\n", p_cfg->ui8compressor);
9279 }
9280 
9281 /*!******************************************************************************
9282  * \brief Setup and initialize all xcoder configuration to default (Rev. B)
9283  *
9284  * \param
9285  *
9286  * \return
9287  ******************************************************************************/
9289 {
9290  uint8_t i = 0;
9291 
9292  if( (!p_ctx) || (!p_config) )
9293  {
9294  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
9295  __func__);
9296  return;
9297  }
9298 
9299  memset(p_config, 0, sizeof(ni_encoder_config_t));
9300 
9301  // fill in common attributes values
9302  p_config->i32picWidth = 720;
9303  p_config->i32picHeight = 480;
9304  p_config->i32meBlkMode = 0; // (AVC ONLY) 0 means use all possible block partitions
9305  p_config->ui8sliceMode = 0; // 0 means 1 slice per picture
9306  p_config->i32frameRateInfo = 30;
9307  p_config->i32frameRateDenominator = 1;
9308  p_config->i32vbvBufferSize = 0; //0; parameter is ignored if rate control is off, if rate control is on, 0 means do not check vbv constraints
9309  p_config->i32userQpMax = 51; // this should also be h264-only parameter
9310 
9311  // AVC only
9312  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9313  {
9314  p_config->i32maxIntraSize = 8000000; // how big an intra p_frame can get?
9315  p_config->i32userMaxDeltaQp = 51;
9316  p_config->i32userMinDeltaQp = 51;
9317  p_config->i32userQpMin = 8;
9318  }
9319 
9320  p_config->i32bitRate = 0;
9321  p_config->i32bitRateBL = 0;
9322  p_config->ui8rcEnable = 0;
9323  p_config->i32srcBitDepth = p_ctx->src_bit_depth;
9324  p_config->ui8enablePTS = 0;
9325  p_config->ui8lowLatencyMode = 0;
9326 
9327  // profiles for H.264: 1 = baseline, 2 = main, 3 = extended, 4 = high
9328  // 5 = high10 (default 8 bit: 4, 10 bit: 5)
9329  // profiles for HEVC: 1 = main, 2 = main10 (default 8 bit: 1, 10 bit: 2)
9330 
9331  // bitstream type: H.264 or HEVC
9332  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9333  {
9334  p_config->ui8bitstreamFormat = STD_AVC;
9335 
9336  p_config->niParamT408.profile = 4;
9337  if (10 == p_ctx->src_bit_depth)
9338  {
9339  p_config->niParamT408.profile = 5;
9340  }
9341  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
9342  {
9343  p_config->ui8bitstreamFormat = STD_JPEG;
9344  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
9345  {
9346  p_config->ui8bitstreamFormat = STD_AV1;
9347 
9348  p_config->niParamT408.profile = 1;
9349  } else
9350  {
9352 
9353  p_config->ui8bitstreamFormat = STD_HEVC;
9354 
9355  p_config->niParamT408.profile = 1;
9356  if (10 == p_ctx->src_bit_depth)
9357  {
9358  p_config->niParamT408.profile = 2;
9359  }
9360  }
9361 
9362  p_config->hdrEnableVUI = 0;
9363  p_config->ui32sourceEndian = p_ctx->src_endian;
9364 
9365  p_config->niParamT408.level = 0;
9366  p_config->niParamT408.tier = 0; // 0 means main tier
9367 
9368  p_config->niParamT408.internalBitDepth = p_ctx->src_bit_depth;
9369  p_config->niParamT408.losslessEnable = 0;
9370  p_config->niParamT408.constIntraPredFlag = 0;
9371 
9372  if (QUADRA)
9374  else
9376 
9377  p_config->niParamT408.decoding_refresh_type = 1;
9378  p_config->niParamT408.intra_qp = 22;
9379  // avcIdrPeriod (H.264 on T408), NOT shared with intra_period
9380  p_config->niParamT408.intra_period = 120;
9381  p_config->niParamT408.avcIdrPeriod = 120;
9382 
9383  p_config->niParamT408.conf_win_top = 0;
9384  p_config->niParamT408.conf_win_bottom = 0;
9385  p_config->niParamT408.conf_win_left = 0;
9386  p_config->niParamT408.conf_win_right = 0;
9387 
9388  p_config->niParamT408.independSliceMode = 0;
9389  p_config->niParamT408.independSliceModeArg = 0;
9390  p_config->niParamT408.dependSliceMode = 0;
9391  p_config->niParamT408.dependSliceModeArg = 0;
9392  p_config->niParamT408.intraRefreshMode = 0;
9393  p_config->niParamT408.intraRefreshArg = 0;
9394 
9395  p_config->niParamT408.use_recommend_enc_params = 0; //1;
9396  p_config->niParamT408.scalingListEnable = 0;
9397 
9398  p_config->niParamT408.cu_size_mode = 7;
9399  p_config->niParamT408.tmvpEnable = 1;
9400  p_config->niParamT408.wppEnable = 0;
9401  p_config->niParamT408.max_num_merge = 2;
9402  p_config->niParamT408.disableDeblk = 0;
9404  p_config->niParamT408.betaOffsetDiv2 = 0;
9405  p_config->niParamT408.tcOffsetDiv2 = 0;
9406  p_config->niParamT408.skipIntraTrans = 1;
9407  p_config->niParamT408.saoEnable = 1;
9408  p_config->niParamT408.intraNxNEnable = 1;
9409 
9410  p_config->niParamT408.bitAllocMode = 0;
9411 
9412  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9413  {
9414  p_config->niParamT408.fixedBitRatio[i] = 1;
9415  }
9416 
9417  if (QUADRA)
9419  else
9421 
9422  p_config->niParamT408.enable_hvs_qp = 0;
9423  p_config->niParamT408.hvs_qp_scale = 2;
9424 
9425  p_config->niParamT408.max_delta_qp = 10;
9426 
9427  // CUSTOM_GOP
9429 #ifndef QUADRA
9430  if (!QUADRA)
9431  {
9432  for (i = 0; i < p_config->niParamT408.custom_gop_params.custom_gop_size; i++)
9433  {
9436  p_config->niParamT408.custom_gop_params.pic_param[i].pic_qp = 0;
9437  p_config->niParamT408.custom_gop_params.pic_param[i].num_ref_pic_L0 = 0;
9438  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L0 = 0;
9439  p_config->niParamT408.custom_gop_params.pic_param[i].ref_poc_L1 = 0;
9441  }
9442  }
9443  else // QUADRA
9444 #endif
9445  {
9446  int j;
9447  for (i = 0; i < NI_MAX_GOP_NUM; i++)
9448  {
9452  (float)0.3; // QP Factor range is between 0.3 and 1, higher values mean lower quality and less bits
9456  for (j = 0; j < NI_MAX_REF_PIC; j++)
9457  {
9458  p_config->niParamT408.custom_gop_params.pic_param[i].rps[j].ref_pic = 0;
9460  }
9461  }
9462  }
9463 
9464  p_config->niParamT408.roiEnable = 0;
9465 
9466  p_config->niParamT408.numUnitsInTick = 1000;
9467  p_config->niParamT408.timeScale = p_config->i32frameRateInfo * 1000;
9468  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
9469  {
9470  p_config->niParamT408.timeScale *= 2;
9471  }
9472 
9473  p_config->niParamT408.numTicksPocDiffOne = 0;
9474 
9475  p_config->niParamT408.chromaCbQpOffset = 0;
9476  p_config->niParamT408.chromaCrQpOffset = 0;
9477 
9478  p_config->niParamT408.initialRcQp = 63; //-1;
9479 
9480  p_config->niParamT408.nrYEnable = 0;
9481  p_config->niParamT408.nrCbEnable = 0;
9482  p_config->niParamT408.nrCrEnable = 0;
9483 
9484  // ENC_NR_WEIGHT
9485  p_config->niParamT408.nrIntraWeightY = 7;
9486  p_config->niParamT408.nrIntraWeightCb = 7;
9487  p_config->niParamT408.nrIntraWeightCr = 7;
9488  p_config->niParamT408.nrInterWeightY = 4;
9489  p_config->niParamT408.nrInterWeightCb = 4;
9490  p_config->niParamT408.nrInterWeightCr = 4;
9491 
9492  p_config->niParamT408.nrNoiseEstEnable = 0;
9493  p_config->niParamT408.nrNoiseSigmaY = 0;
9494  p_config->niParamT408.nrNoiseSigmaCb = 0;
9495  p_config->niParamT408.nrNoiseSigmaCr = 0;
9496 
9497  p_config->niParamT408.useLongTerm = 0;
9498 
9499  // newly added for T408
9500  p_config->niParamT408.monochromeEnable = 0;
9501  p_config->niParamT408.strongIntraSmoothEnable = 1;
9502 
9503  p_config->niParamT408.weightPredEnable = 0;
9504  p_config->niParamT408.bgDetectEnable = 0;
9505  p_config->niParamT408.bgThrDiff = 8; // matching the C-model
9506  p_config->niParamT408.bgThrMeanDiff = 1; // matching the C-model
9507  p_config->niParamT408.bgLambdaQp = 32; // matching the C-model
9508  p_config->niParamT408.bgDeltaQp = 3; // matching the C-model
9509 
9510  p_config->niParamT408.customLambdaEnable = 0;
9511  p_config->niParamT408.customMDEnable = 0;
9512  p_config->niParamT408.pu04DeltaRate = 0;
9513  p_config->niParamT408.pu08DeltaRate = 0;
9514  p_config->niParamT408.pu16DeltaRate = 0;
9515  p_config->niParamT408.pu32DeltaRate = 0;
9516  p_config->niParamT408.pu04IntraPlanarDeltaRate = 0;
9517  p_config->niParamT408.pu04IntraDcDeltaRate = 0;
9518  p_config->niParamT408.pu04IntraAngleDeltaRate = 0;
9519  p_config->niParamT408.pu08IntraPlanarDeltaRate = 0;
9520  p_config->niParamT408.pu08IntraDcDeltaRate = 0;
9521  p_config->niParamT408.pu08IntraAngleDeltaRate = 0;
9522  p_config->niParamT408.pu16IntraPlanarDeltaRate = 0;
9523  p_config->niParamT408.pu16IntraDcDeltaRate = 0;
9524  p_config->niParamT408.pu16IntraAngleDeltaRate = 0;
9525  p_config->niParamT408.pu32IntraPlanarDeltaRate = 0;
9526  p_config->niParamT408.pu32IntraDcDeltaRate = 0;
9527  p_config->niParamT408.pu32IntraAngleDeltaRate = 0;
9528  p_config->niParamT408.cu08IntraDeltaRate = 0;
9529  p_config->niParamT408.cu08InterDeltaRate = 0;
9530  p_config->niParamT408.cu08MergeDeltaRate = 0;
9531  p_config->niParamT408.cu16IntraDeltaRate = 0;
9532  p_config->niParamT408.cu16InterDeltaRate = 0;
9533  p_config->niParamT408.cu16MergeDeltaRate = 0;
9534  p_config->niParamT408.cu32IntraDeltaRate = 0;
9535  p_config->niParamT408.cu32InterDeltaRate = 0;
9536  p_config->niParamT408.cu32MergeDeltaRate = 0;
9537  p_config->niParamT408.coefClearDisable = 0;
9538  p_config->niParamT408.minQpI = 8;
9539  p_config->niParamT408.maxQpI = 51;
9540  p_config->niParamT408.minQpP = 8;
9541  p_config->niParamT408.maxQpP = 51;
9542  p_config->niParamT408.minQpB = 8;
9543  p_config->niParamT408.maxQpB = 51;
9544 
9545  // for H.264 on T408
9546  p_config->niParamT408.rdoSkip = 0;
9547  p_config->niParamT408.lambdaScalingEnable = 0;
9548  p_config->niParamT408.enable_transform_8x8 = 1;
9549  p_config->niParamT408.slice_mode = 0;
9550  p_config->niParamT408.slice_arg = 0;
9551  p_config->niParamT408.intra_mb_refresh_mode = 0;
9552  p_config->niParamT408.intra_mb_refresh_arg = 0;
9553  if (QUADRA)
9554  p_config->niParamT408.enable_mb_level_rc = 0;
9555  else
9556  p_config->niParamT408.enable_mb_level_rc = 1;
9557  p_config->niParamT408.entropy_coding_mode = 1; // 1 means CABAC, make sure profile is main or above, can't have CABAC in baseline
9558  p_config->niParamT408.forcedHeaderEnable = 0; // first IDR frame
9559 
9560  //QUADRA
9561  p_config->ui8EnableAUD = 0;
9562  p_config->ui8LookAheadDepth = 0;
9563  p_config->ui8rdoLevel = (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format) ? 0 : 1;
9564  p_config->i8crf = -1;
9565  p_config->ui16HDR10MaxLight = 0;
9566  p_config->ui16HDR10AveLight = 0;
9567  p_config->ui8HDR10CLLEnable = 0;
9568  p_config->ui8EnableRdoQuant = 0;
9569  p_config->ui8ctbRcMode = 0;
9570  p_config->ui8gopSize = 0;
9571  p_config->ui8useLowDelayPocType = 0;
9572  p_config->ui8gopLowdelay = 0;
9573  p_config->ui16gdrDuration = 0;
9574  p_config->ui8hrdEnable = 0;
9575  p_config->ui8colorDescPresent = 0;
9577  p_config->ui8colorPrimaries = 2;
9578  //AVCOL_TRC_UNSPECIFIED
9579  p_config->ui8colorTrc = 2;
9580  //AVCOL_SPC_UNSPECIFIED
9581  p_config->ui8colorSpace = 2;
9582  p_config->ui16aspectRatioWidth = 0;
9583  p_config->ui16aspectRatioHeight = 0;
9585  p_config->ui8PixelFormat = NI_PIX_FMT_YUV420P;
9586  p_config->ui32ltrRefInterval = 0;
9587  p_config->i32ltrRefQpOffset = 0;
9588  p_config->ui32ltrFirstGap= 0;
9589  p_config->ui32ltrNextInterval = 1;
9590  p_config->ui8multicoreJointMode = 0;
9591  p_config->ui8videoFullRange = 0;
9592  p_config->ui32setLongTermInterval = 0;
9593  p_config->ui8setLongTermCount = 2;
9594  p_config->ui32QLevel = -1;
9595  p_config->i8chromaQpOffset = 0;
9596  p_config->i32tolCtbRcInter = (int32_t)(0.1 * 1000);
9597  p_config->i32tolCtbRcIntra = (int32_t)(0.1 * 1000);
9598  p_config->i16bitrateWindow = -255;
9599  p_config->ui8inLoopDSRatio = 1;
9600  p_config->ui8blockRCSize = 0;
9601  p_config->ui8rcQpDeltaRange = 10;
9602  p_config->i16ctbRowQpStep = 0;
9603  p_config->ui8NewRCEnable = 255;
9604  p_config->ui8LowDelay = 0;
9605  p_config->ui8fixedframerate = 1;
9606  p_config->ui8enableSSIM = 0;
9607  p_config->ui8hdr10_enable = 0;
9608  p_config->ui16hdr10_dx0 = 0;
9609  p_config->ui16hdr10_dy0 = 0;
9610  p_config->ui16hdr10_dx1 = 0;
9611  p_config->ui16hdr10_dy1 = 0;
9612  p_config->ui16hdr10_dx2 = 0;
9613  p_config->ui16hdr10_dy2 = 0;
9614  p_config->ui16hdr10_wx = 0;
9615  p_config->ui16hdr10_wy = 0;
9616  p_config->ui32hdr10_maxluma = 0;
9617  p_config->ui32hdr10_minluma = 0;
9618  p_config->ui8av1ErrResilientMode = 0;
9619  p_config->ui8intraResetRefresh = 0;
9620  p_config->ui8temporalLayersEnable = 0;
9621  p_config->ui8enable2PassGopPatern = 0;
9622  p_config->i8crfMax = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9623  p_config->i32qcomp = (int32_t)(0.6 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9624  p_config->ui8noMbtree = 0;
9625  p_config->ui8noHWMultiPassSupport = 0;
9626  p_config->i8cuTreeFactor = 5;
9627  p_config->i32ipRatio = (int32_t)(1.4 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9628  p_config->i32pbRatio = (int32_t)(1.3 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9629  p_config->i32cplxDecay = (int32_t)(0.5 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9630  p_config->ui32vbvMaxRate = 0;
9631  p_config->i8ppsInitQp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9632  p_config->ui8bitrateMode = 0; // bitrateMode 0 = max bitrate (actual bitrate can be lower than bitrate target), bitrateMode 1 = average bitrate (actual bitrate approximately equal to bitrate target)
9633  p_config->i8pass1Qp = (int8_t)(-1) + 1; // for old libxcoder backward compatibility, use 1 to represent 0
9634  p_config->i8crfDecimal = 0;
9635  p_config->i8hvsBaseMbComplexity = (int8_t)(15)-15; // for old libxcoder backward compatibility, use -15 to represent 0
9636  p_config->i8statisticOutputLevel = 0;
9637  p_config->i8enableipRatio = 0;
9638  p_config->ui16iFrameSizeRatio = 100;
9639  p_config->ui8compressor = 3;
9640  p_config->u8skipFrameInterval = 0;
9641  p_config->ui8crfMaxIframeEnable = 0;
9642  p_config->ui32vbvMinRate = 0;
9643  p_config->ui8disableBframeRDOQ = 0;
9644  p_config->i32forceBframeQpFactor = (int32_t)(-1.0 * 1000) + 1000; // for old libxcoder backward compatibility, use 1000 to represent 0
9645  p_config->ui8tuneBframeVisual = 0;
9646  p_config->ui8EnableAcqLimit = 0;
9647 }
9648 
9649 /*!******************************************************************************
9650 * \brief Perform validation on custom dec parameters (Rev. B)
9651 *
9652 * \param
9653 *
9654 * \return
9655 ******************************************************************************/
9657  ni_session_context_t *p_ctx,
9658  ni_decoder_config_t *p_cfg,
9659  char *p_param_err,
9660  uint32_t max_err_len)
9661 {
9662  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
9663  ni_retcode_t warning = NI_RETCODE_SUCCESS;
9664  int w = p_src->source_width;
9665  int h = p_src->source_height;
9666  char *p_param_warn = NULL;
9667  int i;
9668 
9669  if (!p_ctx || !p_cfg || !p_param_err)
9670  {
9671  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
9672  __func__);
9673  param_ret = NI_RETCODE_INVALID_PARAM;
9674  LRETURN;
9675  }
9676 
9677  // Zero out the error buffer
9678  p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
9679  if (!p_param_warn)
9680  {
9681  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() p_param_warn malloc failure\n",
9682  NI_ERRNO, __func__);
9683  param_ret = NI_RETCODE_ERROR_MEM_ALOC;
9684  LRETURN;
9685  }
9686 
9687  memset(p_param_err, 0, max_err_len);
9688  memset(p_param_warn, 0, max_err_len);
9689 
9690 
9691  //if (p_cfg->i32bitRate > NI_MAX_BITRATE)
9692  //{
9693  // strncpy(p_param_err, "Invalid i32bitRate: too big", max_err_len);
9694  // param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
9695  // LRETURN;
9696  //}
9697 
9698  if (p_cfg->ui8HWFrame == 0 &&
9699  (p_cfg->asOutputConfig[1].ui8Enabled || p_cfg->asOutputConfig[2].ui8Enabled))
9700  {
9701  strncpy(p_param_err, "Incompatible output format: hw frame must be used if out1 or out2 used", max_err_len);
9702  param_ret = NI_RETCODE_INVALID_PARAM;
9703  LRETURN;
9704  }
9705 
9706  if (p_src->ddr_priority_mode >= 0)
9707  {
9709  "6e") < 0)
9710  {
9711  strncpy(p_param_err, "ddr_priority_mode not supported on device with FW api version < 6.e",
9712  max_err_len);
9714  LRETURN;
9715  }
9716  }
9717 
9718  for (i = 0; i < NI_MAX_NUM_OF_DECODER_OUTPUTS; i++)
9719  {
9720  //checking cropping param
9722  {
9725  {
9726  strncpy(p_param_err, "Invalid crop offset: extends past 48x48 minimum window", max_err_len);
9727  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
9728  LRETURN;
9729  }
9732  {
9733  strncpy(p_param_err, "Invalid crop w or h: must be at least 48x48 minimum window", max_err_len);
9735  LRETURN;
9736  }
9737  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W > w ||
9738  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H > h)
9739  {
9740  strncpy(p_param_err, "Invalid crop w or h: must be smaller than input", max_err_len);
9741  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
9742  LRETURN;
9743  }
9746  {
9747  strncpy(p_param_err, "Invalid crop rect: must fit in input", max_err_len);
9748  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
9749  LRETURN;
9750  }
9751  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16X & 1 ||
9752  p_cfg->asOutputConfig[i].sCroppingRectable.ui16Y & 1 ||
9753  p_cfg->asOutputConfig[i].sCroppingRectable.ui16W & 1 ||
9754  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H & 1)
9755  {
9756  strncpy(p_param_err, "Invalid crop value: even values only", max_err_len);
9757  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9758  LRETURN;
9759  }
9760  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
9761  {
9762  strncpy(p_param_warn, "crop param used but output not enabled!", max_err_len);
9763  warning = NI_RETCODE_PARAM_WARN;
9764  }
9765 
9766  }
9767 
9768  //checking scaling param
9769  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
9770  {
9771  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height == 0 ||
9773  {
9774  if (p_cfg->asOutputConfig[i].ui8EnablePpuScaleAdapt)
9775  {
9776  //may need to revisit if based on cropped input or base input for w/h limit
9777  strncpy(p_param_err, "Invalid scale dimensions: zero", max_err_len);
9779  LRETURN;
9780  }
9781  }
9782  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height & 1 ||
9784  {
9785  strncpy(p_param_err, "Invalid scale value: even values only", max_err_len);
9786  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9787  LRETURN;
9788  }
9790  {
9791  //reject if scale dimensions exceed crop dimensions
9794  {
9795  strncpy(p_param_err, "Invalid scale dimensions: downscale only after cropping", max_err_len);
9796  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
9797  LRETURN;
9798  }
9799  }
9800  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
9801  {
9802  strncpy(p_param_warn, "scale param used but output not enabled!", max_err_len);
9803  warning = NI_RETCODE_PARAM_WARN;
9804  }
9805  }
9806  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
9807  {
9809  {
9810  strncpy(p_param_warn, "force8bit or semiPlanar used but output not enabled!", max_err_len);
9811  warning = NI_RETCODE_PARAM_WARN;
9812  }
9813  }
9814 
9815  //check tiled format compatibility
9817  {
9818  if (p_cfg->asOutputConfig[i].ui8Enabled == 0)
9819  {
9820  continue;
9821  }
9822  if (p_cfg->ui8HWFrame == 0)
9823  {
9824  strncpy(p_param_err,
9825  "Invalid pairing: out=HW must be set with tiled format",
9826  max_err_len);
9827  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9828  LRETURN;
9829  }
9830  if (p_ctx->codec_format == NI_CODEC_FORMAT_VP9)
9831  {
9832  strncpy(p_param_err, "Invalid pairing: VP9 not compatible with tiled format",
9833  max_err_len);
9834  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9835  LRETURN;
9836  }
9837  if (p_cfg->asOutputConfig[i].ui8ScaleEnabled)
9838  {
9839  //only no need to check crop compat if scale is set
9840  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Height % 4 ||
9842  {
9843  strncpy(p_param_err,
9844  "Invalid scale height: mult of 4 only, >= 128",
9845  max_err_len);
9846  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9847  LRETURN;
9848  }
9849  if (p_cfg->asOutputConfig[i].sOutputPictureSize.ui16Width % 4 ||
9851  {
9852  //minimum supported dec is 128 but min enc is 144 so round up
9853  strncpy(p_param_err,
9854  "Invalid scale width: mult of 128 only, >= 144",
9855  max_err_len);
9856  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9857  LRETURN;
9858  }
9859  }
9860  else if (p_cfg->asOutputConfig[i].ui8CropMode == NI_DEC_CROP_MODE_MANUAL)
9861  {
9862  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16H % 4 ||
9863  p_cfg->asOutputConfig[i].sCroppingRectable.ui16H < 128)
9864  {
9865  strncpy(p_param_err,
9866  "Invalid crop height: mult of 4 only, >= 128",
9867  max_err_len);
9868  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9869  LRETURN;
9870  }
9871  if (p_cfg->asOutputConfig[i].sCroppingRectable.ui16W % 4 ||
9873  {
9874  //minimum supported dec is 128 but min enc is 144 so round up
9875  strncpy(p_param_err,
9876  "Invalid crop width: mult of 128 only, >= 144",
9877  max_err_len);
9878  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9879  LRETURN;
9880  }
9881  }
9882  if (p_cfg->asOutputConfig[i].ui8Force8Bit)
9883  {
9884  strncpy(p_param_err, "Force 8 bit: not supported with tiled format\n",
9885  max_err_len);
9886  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
9887  LRETURN;
9888  }
9889  }
9890 
9891  }//end forloop
9892  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
9893  {
9894  param_ret = NI_RETCODE_PARAM_WARN;
9895  strncpy(p_param_err, p_param_warn, max_err_len);
9896  }
9897 
9898 END:
9899 
9900  free(p_param_warn);
9901  return param_ret;
9902 }
9903 
9904 // Check encoder level parameters
9905 static ni_retcode_t ni_check_level(int level, int codec_id)
9906 {
9907  const int l_levels_264[] = {10, 11, 12, 13, 20, 21, 22, 30, 31, 32,
9908  40, 41, 42, 50, 51, 52, 60, 61, 62, 0};
9909  const int l_levels_265[] = {10, 20, 21, 30, 31, 40, 41,
9910  50, 51, 52, 60, 61, 62, 0};
9911  const int l_levels_av1[] = {20, 21, 30, 31, 40, 41, 50, 51, 0};
9912  const int *l_levels = l_levels_264;
9913 
9914  if (level == 0)
9915  {
9916  return NI_RETCODE_SUCCESS;
9917  }
9918 
9919  if (codec_id == NI_CODEC_FORMAT_H265)
9920  {
9921  l_levels = l_levels_265;
9922  } else if (codec_id == NI_CODEC_FORMAT_AV1)
9923  {
9924  l_levels = l_levels_av1;
9925  }
9926 
9927  while (*l_levels != 0)
9928  {
9929  if (*l_levels == level)
9930  {
9931  return NI_RETCODE_SUCCESS;
9932  }
9933  l_levels++;
9934  }
9935 
9936  return NI_RETCODE_FAILURE;
9937 }
9938 
9939 /*!******************************************************************************
9940  * \brief Perform validation on custom parameters (Rev. B)
9941  *
9942  * \param
9943  *
9944  * \return
9945  ******************************************************************************/
9947  ni_encoder_config_t *p_cfg,
9948  ni_xcoder_params_t *p_src,
9949  char *p_param_err,
9950  uint32_t max_err_len)
9951 {
9952  ni_retcode_t param_ret = NI_RETCODE_SUCCESS;
9953  ni_retcode_t warning = NI_RETCODE_SUCCESS;
9954  char* p_param_warn = malloc(sizeof(p_ctx->param_err_msg));
9955  ni_encoder_cfg_params_t *p_enc;
9956  int i;
9957 
9958  if( (!p_ctx) || (!p_cfg) || (!p_src) || (!p_param_err) )
9959  {
9960  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
9961  __func__);
9962  param_ret = NI_RETCODE_INVALID_PARAM;
9963  LRETURN;
9964  }
9965 
9966  //Zero out the error buffer
9967  p_enc = &p_src->cfg_enc_params;
9968  memset(p_param_err, 0, max_err_len);
9969  memset(p_param_warn, 0, max_err_len);
9970 
9971  if (0 == p_cfg->i32frameRateInfo)
9972  {
9973  strncpy(p_param_err, "Invalid frame_rate of 0 value", max_err_len);
9974  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
9975  LRETURN;
9976  }
9977 
9978  if (((p_cfg->i32frameRateInfo + p_cfg->i32frameRateDenominator - 1) /
9980  {
9981  strncpy(p_param_err, "Invalid i32frameRateInfo: too big", max_err_len);
9982  param_ret = NI_RETCODE_PARAM_ERROR_FRATE;
9983  LRETURN;
9984  }
9985 
9986  if (p_cfg->i32bitRate <= p_cfg->i32frameRateInfo)
9987  {
9988  strncpy(p_param_err, "Invalid i32bitRate: smaller than or equal to frame rate", max_err_len);
9989  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
9990  LRETURN;
9991  }
9992 
9993  if (p_cfg->i32bitRate > NI_MAX_BITRATE)
9994  {
9995  strncpy(p_param_err, "Invalid i32bitRate: too big", max_err_len);
9996  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
9997  LRETURN;
9998  }
9999 
10000  if (p_cfg->i32bitRate < NI_MIN_BITRATE )
10001  {
10002  strncpy(p_param_err, "Invalid i32bitRate: too low", max_err_len);
10003  param_ret = NI_RETCODE_PARAM_ERROR_BRATE;
10004  LRETURN;
10005  }
10006 
10008  {
10009  strncpy(p_param_err, "Invalid Picture Width: too small", max_err_len);
10011  LRETURN;
10012  }
10013 
10015  {
10016  strncpy(p_param_err, "Invalid Picture Width: too big", max_err_len);
10018  LRETURN;
10019  }
10020 
10022  {
10023  strncpy(p_param_err, "Invalid Picture Height: too small", max_err_len);
10025  LRETURN;
10026  }
10027 
10029  {
10030  strncpy(p_param_err, "Invalid Picture Height: too big", max_err_len);
10032  LRETURN;
10033  }
10034 
10035  if (p_cfg->ui32cropWidth || p_cfg->ui32cropHeight)
10036  {
10037  // check cropping width & height are both non-zero
10038  if (!p_cfg->ui32cropWidth || !p_cfg->ui32cropHeight)
10039  {
10040  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %u): both need to be specified",
10041  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10042  param_ret = NI_RETCODE_INVALID_PARAM;
10043  LRETURN;
10044  }
10045  // check cropping width & height are even
10046  if ((p_cfg->ui32cropWidth % 2) || (p_cfg->ui32cropHeight % 2))
10047  {
10048  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height (%u x %uu): must be even",
10049  p_cfg->ui32cropWidth, p_cfg->ui32cropHeight);
10050  param_ret = NI_RETCODE_INVALID_PARAM;
10051  LRETURN;
10052  }
10053  // check cropping width & height meet resoultion constraint (including AV1 max resoultion)
10054  if (p_cfg->ui32cropWidth < NI_MIN_WIDTH)
10055  {
10056  snprintf(p_param_err, max_err_len, "Invalid Crop Width: less than %d",
10057  NI_MIN_WIDTH);
10059  LRETURN;
10060  }
10061  if (p_cfg->ui32cropHeight < NI_MIN_HEIGHT)
10062  {
10063  snprintf(p_param_err, max_err_len, "Invalid Crop Height: less than %d",
10064  NI_MIN_HEIGHT);
10066  LRETURN;
10067  }
10068  if (p_cfg->ui32cropWidth > NI_PARAM_MAX_WIDTH)
10069  {
10070  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10073  LRETURN;
10074  }
10075  if (p_cfg->ui32cropHeight > NI_PARAM_MAX_HEIGHT)
10076  {
10077  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10080  LRETURN;
10081  }
10082  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_MAX_RESOLUTION_AREA)
10083  {
10084  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10087  LRETURN;
10088  }
10089 
10090  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10091  {
10093  {
10094  snprintf(p_param_err, max_err_len, "Invalid Crop Width: exceeds %d",
10097  LRETURN;
10098  }
10100  {
10101  snprintf(p_param_err, max_err_len, "Invalid Crop Height: exceeds %d",
10104  LRETURN;
10105  }
10106  if (p_cfg->ui32cropWidth * p_cfg->ui32cropHeight > NI_PARAM_AV1_MAX_AREA)
10107  {
10108  snprintf(p_param_err, max_err_len, "Invalid Crop Width x Height: exceeds %d",
10111  LRETURN;
10112  }
10113  }
10114 
10115  if (p_src->source_width < (p_cfg->ui32horOffset + p_cfg->ui32cropWidth))
10116  {
10117  strncpy(p_param_err, "Invalid Crop Width: offset + crop width too big", max_err_len);
10119  LRETURN;
10120  }
10121  if (p_src->source_height < (p_cfg->ui32verOffset + p_cfg->ui32cropHeight))
10122  {
10123  strncpy(p_param_err, "Invalid Crop Height: offset + crop height too big", max_err_len);
10125  LRETURN;
10126  }
10127  }
10128  else // check source width / height meet AV1 max resoultion constraint if cropping not specified
10129  {
10130  if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10131  {
10132  if (p_src->source_width > NI_PARAM_AV1_MAX_WIDTH)
10133  {
10134  snprintf(p_param_err, max_err_len, "Invalid Picture Width: exceeds %d",
10137  LRETURN;
10138  }
10140  {
10141  snprintf(p_param_err, max_err_len, "Invalid Picture Height: exceeds %d",
10144  LRETURN;
10145  }
10146  if (p_src->source_width * p_src->source_height > NI_PARAM_AV1_MAX_AREA)
10147  {
10148  snprintf(p_param_err, max_err_len, "Invalid Picture Width x Height: exceeds %d",
10151  LRETURN;
10152  }
10153  }
10154  }
10155 
10156  if (p_cfg->ui8planarFormat >= NI_PIXEL_PLANAR_MAX)
10157  {
10158  strncpy(p_param_err, "Invalid input planar format: out of range", max_err_len);
10159  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10160  LRETURN;
10161  }
10163  {
10165  "68") < 0)
10166  {
10167  strncpy(p_param_err, "Invalid input planar format for device with FW api version < 6.8",
10168  max_err_len);
10170  LRETURN;
10171  }
10172  if (p_ctx->auto_dl_handle)
10173  {
10174  strncpy(p_param_err, "Invalid Encoder Selected: Tiled format must be on same device",
10175  max_err_len);
10176  param_ret = NI_RETCODE_ERROR_INVALID_HANDLE;
10177  LRETURN;
10178  }
10179  if (p_src->source_height % 4 != 0)
10180  {
10181  strncpy(p_param_err,
10182  "Invalid Picture Height: tiled format only supports "
10183  "multiples of 4",
10184  max_err_len);
10186  LRETURN;
10187  }
10188  if (p_src->source_width % 4 != 0)
10189  {
10190  strncpy(p_param_err,
10191  "Invalid Picture Width: tiled format only supports "
10192  "multiples of 4",
10193  max_err_len);
10195  LRETURN;
10196  }
10197  }
10198 
10199  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
10200  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA ||
10201  p_cfg->ui8PixelFormat == NI_PIX_FMT_ABGR ||
10202  p_cfg->ui8PixelFormat == NI_PIX_FMT_ARGB)
10203  {
10204  if (p_src->zerocopy_mode == 0)
10205  {
10206  strncpy(p_param_err, "zeroCopyMode must not be disabled for RGBA / BGRA / ABGR / ARGB pixel formats", max_err_len);
10207  param_ret = NI_RETCODE_INVALID_PARAM;
10208  LRETURN;
10209  }
10210  if (p_cfg->ui8PixelFormat == NI_PIX_FMT_RGBA ||
10211  p_cfg->ui8PixelFormat == NI_PIX_FMT_BGRA)
10212  {
10214  "6Y") < 0)
10215  {
10216  strncpy(p_param_err, "RGBA / BGRA pixel formats not supported on device with FW api version < 6.Y",
10217  max_err_len);
10219  LRETURN;
10220  }
10221  }
10222  }
10223 
10224  if (p_src->ddr_priority_mode >= 0)
10225  {
10227  "6e") < 0)
10228  {
10229  strncpy(p_param_err, "ddr_priority_mode not supported on device with FW api version < 6.e",
10230  max_err_len);
10232  LRETURN;
10233  }
10234  }
10235 
10236  if (NI_RETCODE_SUCCESS !=
10237  ni_check_level(p_src->cfg_enc_params.level_idc, p_ctx->codec_format))
10238  {
10239  strncpy(p_param_err, "Invalid Encoder Level: out of range", max_err_len);
10240  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10241  LRETURN;
10242  }
10243 
10244  if ((p_src->cfg_enc_params.intra_mb_refresh_mode < 0 ||
10245  p_src->cfg_enc_params.intra_mb_refresh_mode > 4) ||
10246  (NI_CODEC_FORMAT_H264 == p_ctx->codec_format &&
10248  {
10249  strncpy(p_param_err, "Invalid intra_mb_refresh_mode: out of range",
10250  max_err_len);
10251  param_ret = NI_RETCODE_PARAM_ERROR_OOR;
10252  LRETURN;
10253  }
10254 
10255  if (!QUADRA)
10256  {
10258  {
10260  {
10261  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len);
10263  LRETURN;
10264  }
10267  {
10268  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len);
10270  LRETURN;
10271  }
10272  }
10273  }
10274  else // QUADRA
10275  {
10277  {
10279  {
10280  strncpy(p_param_err,
10281  "Invalid custom GOP paramaters: custom gop size must > 0",
10282  max_err_len);
10284  LRETURN;
10285  }
10286  }
10288  {
10291  {
10292  strncpy(p_param_err,
10293  "Invalid custom GOP paramaters: selected gopPresetIdx is "
10294  "not compatible with custom gop",
10295  max_err_len);
10297  LRETURN;
10298  }
10300  {
10301  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too small", max_err_len);
10303  LRETURN;
10304  }
10307  {
10308  strncpy(p_param_err, "Invalid custom GOP paramaters: custom_gop_size too big", max_err_len);
10310  LRETURN;
10311  }
10312 
10313  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
10314  {
10317  {
10318  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset larger"
10319  " than GOP size", max_err_len);
10321  LRETURN;
10322  }
10323  }
10324  }
10325 
10326  if(p_cfg->ui8useLowDelayPocType != 0)
10327  {
10328  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10329  {
10330  p_cfg->ui8useLowDelayPocType = 0;
10331  strncpy(p_param_warn, "useLowDelayPocType is only supported for H.264. Change useLowDelayPocType to 0", max_err_len);
10332  warning = NI_RETCODE_PARAM_WARN;
10333  }
10334  }
10335  if(p_cfg->niParamT408.entropy_coding_mode != 1)
10336  {
10337  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10338  {
10339  strncpy(p_param_err,
10340  "entropyCodingMode is only supported for H.264.",
10341  max_err_len);
10343  LRETURN;
10344  }
10345  }
10346  if (p_cfg->ui8av1ErrResilientMode)
10347  {
10348  if (NI_CODEC_FORMAT_AV1 != p_ctx->codec_format)
10349  {
10350  strncpy(p_param_err,
10351  "av1ErrorResilientMode is only supported for AV1.",
10352  max_err_len);
10354  LRETURN;
10355  }
10356  }
10357  if (-1 != p_cfg->ui32QLevel)
10358  {
10359  if (NI_CODEC_FORMAT_JPEG != p_ctx->codec_format)
10360  {
10361  strncpy(p_param_err, "qLevel is only supported for JPEG.",
10362  max_err_len);
10364  LRETURN;
10365  }
10366  }
10367 
10368  if(p_cfg->ui8enableSSIM != 0)
10369  {
10371  "62") < 0)
10372  {
10373  p_cfg->ui8enableSSIM = 0;
10374  strncpy(p_param_warn, "enableSSIM is not supported on device with FW api version < 6.2. Reported ssim will be 0.", max_err_len);
10375  warning = NI_RETCODE_PARAM_WARN;
10376  }
10377  }
10378 
10379  if (p_cfg->niParamT408.slice_mode || p_cfg->niParamT408.slice_arg)
10380  {
10382  {
10383  strncpy(p_param_err, "sliceMode/sliceArg is only supported for H.264 or H.265.",
10384  max_err_len);
10386  LRETURN;
10387  }
10388 
10389  if (p_cfg->niParamT408.slice_mode)
10390  {
10391  int ctu_mb_size = (NI_CODEC_FORMAT_H264 == p_ctx->codec_format) ? 16 : 64;
10392  int max_num_ctu_mb_row = (p_src->source_height + ctu_mb_size - 1) / ctu_mb_size;
10393  if (p_cfg->niParamT408.slice_arg < 1 || p_cfg->niParamT408.slice_arg > max_num_ctu_mb_row ||
10395  {
10396  snprintf(p_param_err, max_err_len, "Invalid number of rows per slice: should be between 1 and %d",
10397  max_num_ctu_mb_row < NI_MAX_SLICE_SIZE ? max_num_ctu_mb_row : NI_MAX_SLICE_SIZE);
10398  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
10399  LRETURN;
10400  }
10401  }
10402  }
10403  if (p_src->use_low_delay_poc_type)
10404  {
10405  if (NI_CODEC_FORMAT_H264 != p_ctx->codec_format)
10406  {
10407  strncpy(p_param_err, "useLowDelayPocType is only supported for H.264.",
10408  max_err_len);
10410  LRETURN;
10411  }
10412  }
10413  }
10414 
10415  if (QUADRA)
10416  {
10417  if (p_cfg->ui8LookAheadDepth != 0 || p_cfg->i8crf >= 0)
10418  {
10419  if ((1 == p_cfg->niParamT408.gop_preset_index && p_cfg->ui8LookAheadDepth != 0) ||
10420  3 == p_cfg->niParamT408.gop_preset_index ||
10421  7 == p_cfg->niParamT408.gop_preset_index ||
10422  10 == p_cfg->niParamT408.gop_preset_index ||
10423  15 == p_cfg->niParamT408.gop_preset_index)
10424  {
10425  strncpy(p_param_err,
10426  "this gopPreset is not supported for lookahead and/or CRF",
10427  max_err_len);
10429  LRETURN;
10430  }
10431 
10433  {
10434  bool bIsgopLowdelay = true;
10435  // lookahead does not support gopLowDelay (all B-frames, encode in order) gop patterns
10436  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
10437  i++)
10438  {
10439  // check if all frames are B-frames
10440  if (2 != p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
10441  {
10442  bIsgopLowdelay = false;
10443  break;
10444  }
10445  // check if all frames are encoded in display order
10446  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
10447  {
10448  bIsgopLowdelay = false;
10449  break;
10450  }
10451  }
10452  if (bIsgopLowdelay)
10453  {
10454  strncpy(p_param_err, "B-frames low delay custom gop is not supported for "
10455  "lookahead and/or CRF", max_err_len);
10456  param_ret = NI_RETCODE_INVALID_PARAM;
10457  LRETURN;
10458  }
10459  }
10460 
10461  if (p_enc->bitrateMode != -1)
10462  {
10463  strncpy(p_param_err,
10464  "bitrateMode is invalid when lookahead is enabled (or in CRF mode)",
10465  max_err_len);
10467  LRETURN;
10468  }
10469  }
10470 
10472  {
10473  // when gop_preset_index = -1 (default), gop pattern is decided by gopSize and gopLowdelay
10474  //p_cfg->ui8gopSize = 0;
10475  //p_cfg->ui8gopLowdelay = 0;
10476  }
10477  if (1 == p_cfg->niParamT408.gop_preset_index)
10478  {
10479  p_cfg->niParamT408.intra_period = 1;
10480  p_cfg->niParamT408.avcIdrPeriod = 1;
10481  p_cfg->ui8gopSize = 1;
10482  p_cfg->ui16gdrDuration = 0;
10483  }
10484  if (2 == p_cfg->niParamT408.gop_preset_index)
10485  {
10486  strncpy(p_param_err,
10487  "gopPresetIdx 2 is obsolete, suggest to use gopPresetIdx 9 "
10488  "instead",
10489  max_err_len);
10491  LRETURN;
10492  }
10493  if (3 == p_cfg->niParamT408.gop_preset_index)
10494  {
10495  p_cfg->ui8gopSize = 1;
10496  p_cfg->ui8gopLowdelay = 1;
10497  }
10498  if (4 == p_cfg->niParamT408.gop_preset_index)
10499  {
10500  p_cfg->ui8gopSize = 2;
10501  p_cfg->ui8gopLowdelay = 0;
10502  }
10503  if (5 == p_cfg->niParamT408.gop_preset_index)
10504  {
10505  p_cfg->ui8gopSize = 4;
10506  p_cfg->ui8gopLowdelay = 0;
10507  }
10508  if (6 == p_cfg->niParamT408.gop_preset_index)
10509  {
10510  strncpy(p_param_err,
10511  "gopPresetIdx 6 is obsolete, suggest to use gopPresetIdx 7 "
10512  "instead",
10513  max_err_len);
10515  LRETURN;
10516  }
10517  if (7 == p_cfg->niParamT408.gop_preset_index)
10518  {
10519  p_cfg->ui8gopSize = 4;
10520  p_cfg->ui8gopLowdelay = 1;
10521  }
10522  if (8 == p_cfg->niParamT408.gop_preset_index)
10523  {
10524  p_cfg->ui8gopSize = 8;
10525  p_cfg->ui8gopLowdelay = 0;
10526  }
10527  if (9 == p_cfg->niParamT408.gop_preset_index)
10528  {
10529  p_cfg->ui8gopSize = 1;
10530  p_cfg->ui8gopLowdelay = 0;
10531  }
10532  if (10 == p_cfg->niParamT408.gop_preset_index)
10533  {
10534  p_cfg->ui8gopSize = 4;
10535  p_cfg->ui8gopLowdelay = 0;
10536  }
10537  if (15 == p_cfg->niParamT408.gop_preset_index)
10538  {
10539  p_cfg->ui8gopSize = 16;
10540  p_cfg->ui8gopLowdelay = 0;
10541  }
10542 
10543  if (p_cfg->ui8LookAheadDepth != 0
10544  || p_cfg->ui8bitrateMode == 1
10545  || p_cfg->ui32vbvMaxRate
10546  || p_cfg->ui32vbvMinRate
10547  || p_cfg->i8enableipRatio)
10548  {
10549  ni_log2(p_ctx, NI_LOG_DEBUG, "Force newRcEnable to 0 in 2-pass encode or in 1-pass average bitrate mode, with ipRatio, VbvMaxRate, or VbvMinRate\n");
10550  p_cfg->ui8NewRCEnable = 0;
10551  }
10552  else if (p_cfg->ui8NewRCEnable == 255)
10553  {
10554  if ((p_cfg->ui8gopSize > 1 || p_cfg->ui8gopSize == 0) && p_cfg->ui8gopLowdelay == 0)
10555  {
10556  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in non low delay gop preset\n");
10557  p_cfg->ui8NewRCEnable = 0;
10558  }
10559  else if (p_cfg->niParamT408.intra_period == 1 || p_cfg->niParamT408.avcIdrPeriod == 1)
10560  {
10561  ni_log2(p_ctx, NI_LOG_DEBUG, "Set newRcEnable to 0 in intra only mode\n");
10562  p_cfg->ui8NewRCEnable = 0;
10563  }
10564  }
10565  }
10566 
10567  if (NI_CODEC_FORMAT_H264 == p_ctx->codec_format)
10568  {
10569  if (!QUADRA)
10570  {
10571  if (10 == p_ctx->src_bit_depth)
10572  {
10573  if (p_cfg->niParamT408.profile != 5)
10574  {
10575  strncpy(p_param_err, "Invalid profile: must be 5 (high10)",
10576  max_err_len);
10577  param_ret = NI_RETCODE_INVALID_PARAM;
10578  LRETURN;
10579  }
10580  }
10581  else
10582  {
10583  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5)
10584  {
10585  strncpy(p_param_err, "Invalid profile: must be 1 (baseline), 2 (main),"
10586  " 3 (extended), 4 (high), or 5 (high10)", max_err_len);
10587  param_ret = NI_RETCODE_INVALID_PARAM;
10588  LRETURN;
10589  }
10590  }
10591  if (1 == p_cfg->niParamT408.profile &&
10592  ! (0 == p_cfg->niParamT408.gop_preset_index ||
10593  1 == p_cfg->niParamT408.gop_preset_index ||
10594  2 == p_cfg->niParamT408.gop_preset_index ||
10595  6 == p_cfg->niParamT408.gop_preset_index))
10596  {
10597  strncpy(p_param_err, "Invalid gopPresetIdx for H.264 baseline profile:"
10598  " must be 1, 2, 6 or 0 (custom with no B frames)", max_err_len);
10599  param_ret = NI_RETCODE_INVALID_PARAM;
10600  LRETURN;
10601  }
10602  if (1 == p_cfg->niParamT408.profile &&
10604  {
10605  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size;
10606  i++)
10607  {
10608  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
10609  {
10610  strncpy(p_param_err, "H.264 baseline profile: custom GOP can not "
10611  "have B frames", max_err_len);
10612  param_ret = NI_RETCODE_INVALID_PARAM;
10613  LRETURN;
10614  }
10615  }
10616  }
10617  }
10618  else // QUADRA
10619  {
10620  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 5 || p_cfg->niParamT408.profile == 3)
10621  {
10622  strncpy(p_param_err, "Invalid profile: must be 1 (baseline), 2 (main),"
10623  " 4 (high), or 5 (high10)", max_err_len);
10624  param_ret = NI_RETCODE_INVALID_PARAM;
10625  LRETURN;
10626  }
10627  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 5)
10628  {
10629  strncpy(p_param_warn, "AVC Baseline/Main/High Profile do not support 10-bit, auto convert to 8-bit", max_err_len);
10630  warning = NI_RETCODE_PARAM_WARN;
10631  }
10632  if (1 == p_cfg->niParamT408.profile)
10633  {
10635  {
10636  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
10637  {
10638  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
10639  {
10640  strncpy(p_param_err, "H.264 baseline profile: custom GOP can not "
10641  "have B frames", max_err_len);
10642  param_ret = NI_RETCODE_INVALID_PARAM;
10643  LRETURN;
10644  }
10645  }
10646  } else if (((p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)) &&
10647  (p_cfg->niParamT408.intra_period != 1) &&
10649  {
10651  p_cfg->ui8gopSize != 0 ||
10652  p_cfg
10653  ->ui8gopLowdelay) // if gopSize is 0 (default / adapative gop), autoset to 1
10654  {
10655  strncpy(p_param_err,
10656  "Must use gopPresetIdx 1,9,10 (no "
10657  "B frames) for profile 1",
10658  max_err_len);
10659  param_ret = NI_RETCODE_INVALID_PARAM;
10660  LRETURN;
10661  }
10662  p_cfg->ui8gopSize = 1; //autoset to 1
10663  }
10664  }
10665  if (p_cfg->niParamT408.tier)
10666  {
10667  strncpy(p_param_err, "Tier is not supported for H.264", max_err_len);
10668  param_ret = NI_RETCODE_INVALID_PARAM;
10669  LRETURN;
10670  }
10671  }
10672  }
10673  else if (NI_CODEC_FORMAT_H265 == p_ctx->codec_format)
10674  {
10675  if (!QUADRA)
10676  {
10677  if (10 == p_ctx->src_bit_depth)
10678  {
10679  if (p_cfg->niParamT408.profile != 2)
10680  {
10681  strncpy(p_param_err, "Invalid profile: must be 2 (main10)",
10682  max_err_len);
10683  param_ret = NI_RETCODE_INVALID_PARAM;
10684  LRETURN;
10685  }
10686  }
10687  else
10688  {
10689  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
10690  {
10691  strncpy(p_param_err, "Invalid profile: must be 1 (main) or 2 (main10)",
10692  max_err_len);
10693  param_ret = NI_RETCODE_INVALID_PARAM;
10694  LRETURN;
10695  }
10696  }
10697  }
10698  else // QUADRA
10699  {
10700  if (p_cfg->niParamT408.profile < 1 || p_cfg->niParamT408.profile > 2)
10701  {
10702  strncpy(p_param_err, "Invalid profile: must be 1 (main) or 2 (main10)",
10703  max_err_len);
10704  param_ret = NI_RETCODE_INVALID_PARAM;
10705  LRETURN;
10706  }
10707  if (10 == p_ctx->src_bit_depth && p_cfg->niParamT408.profile != 2)
10708  {
10709  strncpy(p_param_warn, "HEVC Main Profile does not support 10-bit, auto convert to 8-bit", max_err_len);
10710  warning = NI_RETCODE_PARAM_WARN;
10711  }
10712  }
10713  } else if (NI_CODEC_FORMAT_AV1 == p_ctx->codec_format)
10714  {
10715  if (p_cfg->niParamT408.profile != 1)
10716  {
10717  strncpy(p_param_err, "Invalid profile: must be 1 (main)",
10718  max_err_len);
10719  param_ret = NI_RETCODE_INVALID_PARAM;
10720  LRETURN;
10721  }
10722  if (p_cfg->niParamT408.level) // 0 means auto level
10723  {
10724  if (p_cfg->niParamT408.level < 20)
10725  {
10726  p_cfg->niParamT408.level = 20;
10727  strncpy(p_param_warn,
10728  "AV1 level < 2.0 is not supported, change to level 2.0",
10729  max_err_len);
10730  warning = NI_RETCODE_PARAM_WARN;
10731  } else if (p_cfg->niParamT408.level > 51)
10732  {
10733  p_cfg->niParamT408.level = 51;
10734  strncpy(p_param_warn,
10735  "AV1 level > 5.1 is not supported, change to level 5.1",
10736  max_err_len);
10737  warning = NI_RETCODE_PARAM_WARN;
10738  }
10739  }
10740 
10741  if (p_cfg->niParamT408.conf_win_top != 0)
10742  {
10744  0;
10745  strncpy(p_param_warn, "confWinTop is not supported in AV1",
10746  max_err_len);
10747  warning = NI_RETCODE_PARAM_WARN;
10748  }
10749  if (p_cfg->niParamT408.conf_win_bottom != 0)
10750  {
10751  p_cfg->niParamT408.conf_win_bottom =
10752  p_src->cfg_enc_params.conf_win_bottom = 0;
10753  strncpy(p_param_warn, "confWinBottom is not supported in AV1",
10754  max_err_len);
10755  warning = NI_RETCODE_PARAM_WARN;
10756  }
10757  if (p_cfg->niParamT408.conf_win_left != 0)
10758  {
10759  p_cfg->niParamT408.conf_win_left =
10760  p_src->cfg_enc_params.conf_win_left = 0;
10761  strncpy(p_param_warn, "confWinLeft is not supported in AV1",
10762  max_err_len);
10763  warning = NI_RETCODE_PARAM_WARN;
10764  }
10765  if (p_cfg->niParamT408.conf_win_right != 0)
10766  {
10767  p_cfg->niParamT408.conf_win_right =
10768  p_src->cfg_enc_params.conf_win_right = 0;
10769  strncpy(p_param_warn, "confWinRight is not supported in AV1",
10770  max_err_len);
10771  warning = NI_RETCODE_PARAM_WARN;
10772  }
10773  if (p_cfg->ui8hdr10_enable)
10774  {
10775  strncpy(p_param_err,
10776  "masterDisplay not supported for AV1",
10777  max_err_len);
10778  warning = NI_RETCODE_PARAM_WARN;
10779  }
10780  if (p_cfg->ui8hrdEnable)
10781  {
10782  strncpy(p_param_err, "hrdEnable is not supported on av1 encoder",
10783  max_err_len);
10785  LRETURN;
10786  }
10787  if (p_cfg->ui8EnableAUD)
10788  {
10789  strncpy(p_param_err, "enableAUD is not supported on av1 encoder",
10790  max_err_len);
10792  LRETURN;
10793  }
10794  if (p_cfg->ui8HDR10CLLEnable)
10795  {
10796  strncpy(p_param_err, "maxCLL is not supported on av1 encoder",
10797  max_err_len);
10799  LRETURN;
10800  }
10801  if (p_cfg->ui8repeatHeaders)
10802  {
10803  strncpy(p_param_err, "repeatHeaders is not supported on av1 encoder",
10804  max_err_len);
10805  warning = NI_RETCODE_PARAM_WARN;
10806  }
10807  if (p_cfg->ui8enableSSIM)
10808  {
10809  strncpy(p_param_err, "enableSSIM is not supported on av1 encoder",
10810  max_err_len);
10812  LRETURN;
10813  }
10814  if (p_cfg->ui8EnableRdoQuant)
10815  {
10816  strncpy(p_param_err, "EnableRdoQuant is not supported on av1 encoder",
10817  max_err_len);
10819  LRETURN;
10820  }
10821  if (p_cfg->ui8fillerEnable)
10822  {
10823  strncpy(p_param_err, "fillerEnable is not supported on av1 encoder",
10824  max_err_len);
10826  LRETURN;
10827  }
10828  if (p_cfg->i8ppsInitQp)
10829  {
10830  strncpy(p_param_err, "ppsInitQp is not supported for av1 encoder",
10831  max_err_len);
10833  LRETURN;
10834  }
10835  } else if (NI_CODEC_FORMAT_JPEG == p_ctx->codec_format)
10836  {
10837  if (p_cfg->ui8hdr10_enable)
10838  {
10839  strncpy(p_param_err,
10840  "masterDisplay not supported for jpeg",
10841  max_err_len);
10842  warning = NI_RETCODE_PARAM_WARN;
10843  }
10844  if (p_cfg->niParamT408.conf_win_top != 0)
10845  {
10846  strncpy(p_param_err, "confWinTop is not supported in jpeg",
10847  max_err_len);
10849  LRETURN;
10850  }
10851  if (p_cfg->niParamT408.conf_win_bottom != 0)
10852  {
10853  strncpy(p_param_err, "confWinBottom is not supported in jpeg",
10854  max_err_len);
10856  LRETURN;
10857  }
10858  if (p_cfg->niParamT408.conf_win_left != 0)
10859  {
10860  strncpy(p_param_err, "confWinLeft is not supported in jpeg",
10861  max_err_len);
10863  LRETURN;
10864  }
10865  if (p_cfg->niParamT408.conf_win_right != 0)
10866  {
10867  strncpy(p_param_err, "confWinRight is not supported in jpeg",
10868  max_err_len);
10870  LRETURN;
10871  }
10872  if (p_cfg->ui8hrdEnable)
10873  {
10874  strncpy(p_param_err, "hrdEnable is not supported on jpeg encoder",
10875  max_err_len);
10877  LRETURN;
10878  }
10879  if (p_cfg->ui8EnableAUD)
10880  {
10881  strncpy(p_param_err, "enableAUD is not supported on jpeg encoder",
10882  max_err_len);
10884  LRETURN;
10885  }
10886  if (p_cfg->ui8repeatHeaders)
10887  {
10888  strncpy(p_param_err, "repeatHeaders is not supported on jpeg encoder",
10889  max_err_len);
10891  LRETURN;
10892  }
10894  {
10895  strncpy(p_param_err, "prefTRC is not supported on jpeg encoder",
10896  max_err_len);
10898  LRETURN;
10899  }
10900  if (p_cfg->ui8HDR10CLLEnable)
10901  {
10902  strncpy(p_param_err, "maxCLL is not supported on jpeg encoder",
10903  max_err_len);
10905  LRETURN;
10906  }
10907  if (p_cfg->ui8colorPrimaries != 2)
10908  {
10909  strncpy(p_param_err, "colorPri is not supported on jpeg encoder",
10910  max_err_len);
10912  LRETURN;
10913  }
10914  if (p_cfg->ui8colorTrc != 2)
10915  {
10916  strncpy(p_param_err, "colorTrc is not supported on jpeg encoder",
10917  max_err_len);
10919  LRETURN;
10920  }
10921  if (p_cfg->ui8colorSpace != 2)
10922  {
10923  strncpy(p_param_err, "colorSpc is not supported on jpeg encoder",
10924  max_err_len);
10926  LRETURN;
10927  }
10928  if (p_src->sar_num)
10929  {
10930  strncpy(p_param_err, "sarNum is not supported on jpeg encoder",
10931  max_err_len);
10933  LRETURN;
10934  }
10935  if (p_src->sar_denom != 1)
10936  {
10937  strncpy(p_param_err, "sarDenom is not supported on jpeg encoder",
10938  max_err_len);
10940  LRETURN;
10941  }
10942  if (p_src->video_full_range_flag != -1)
10943  {
10944  strncpy(p_param_err,
10945  "videoFullRangeFlag is not supported on jpeg encoder",
10946  max_err_len);
10948  LRETURN;
10949  }
10950  if (p_cfg->ui8temporalLayersEnable)
10951  {
10952  strncpy(p_param_err,
10953  "temporalLayersEnable is not supported on jpeg encoder",
10954  max_err_len);
10956  LRETURN;
10957  }
10958  if (p_cfg->ui8LowDelay)
10959  {
10960  strncpy(p_param_err, "LowDelay is not supported on jpeg encoder",
10961  max_err_len);
10963  LRETURN;
10964  }
10965  if (p_cfg->ui8rdoLevel)
10966  {
10967  strncpy(p_param_err, "rdoLevel is not supported on jpeg encoder",
10968  max_err_len);
10970  LRETURN;
10971  }
10972  if (p_cfg->ui8EnableRdoQuant)
10973  {
10974  strncpy(p_param_err, "EnableRdoQuant is not supported on jpeg encoder",
10975  max_err_len);
10977  LRETURN;
10978  }
10979  if (p_cfg->ui8enable2PassGopPatern)
10980  {
10981  strncpy(p_param_err,
10982  "enable2PassGop is not supported on jpeg encoder",
10983  max_err_len);
10985  LRETURN;
10986  }
10987  if (p_cfg->ui8LookAheadDepth)
10988  {
10989  strncpy(p_param_err,
10990  "lookAheadDepth is not supported on jpeg encoder",
10991  max_err_len);
10993  LRETURN;
10994  }
10996  {
10997  strncpy(p_param_err,
10998  "gopPresetIdx is not supported on jpeg encoder",
10999  max_err_len);
11001  LRETURN;
11002  }
11003  if (p_cfg->niParamT408.roiEnable)
11004  {
11005  strncpy(p_param_err, "roiEnable is not supported on jpeg encoder",
11006  max_err_len);
11008  LRETURN;
11009  }
11010  if(p_src->roi_demo_mode)
11011  {
11012  strncpy(p_param_err,
11013  "RoiDemoMode is not supported on jpeg encoder",
11014  max_err_len);
11016  LRETURN;
11017  }
11018  if (p_src->cacheRoi)
11019  {
11020  strncpy(p_param_err, "cacheRoi is not supported on jpeg encoder",
11021  max_err_len);
11023  LRETURN;
11024  }
11026  {
11027  strncpy(p_param_err,
11028  "ReconfDemoMode is not supported on jpeg encoder",
11029  max_err_len);
11031  LRETURN;
11032  }
11033  if (p_cfg->niParamT408.intraRefreshMode)
11034  {
11035  strncpy(p_param_err,
11036  "intraRefreshMode is not supported on jpeg encoder",
11037  max_err_len);
11039  LRETURN;
11040  }
11041  if (p_cfg->niParamT408.intraRefreshArg)
11042  {
11043  strncpy(p_param_err,
11044  "intraRefreshArg is not supported on jpeg encoder",
11045  max_err_len);
11047  LRETURN;
11048  }
11049  if (p_cfg->niParamT408.intra_period != 120)
11050  {
11051  strncpy(p_param_err,
11052  "intraPeriod is not supported on jpeg encoder",
11053  max_err_len);
11055  LRETURN;
11056  }
11057  if (p_cfg->ui8intraResetRefresh)
11058  {
11059  strncpy(
11060  p_param_err,
11061  "IntraRefreshResetOnForceIDR is not supported on jpeg encoder",
11062  max_err_len);
11064  LRETURN;
11065  }
11066  if (p_cfg->niParamT408.useLongTerm)
11067  {
11068  strncpy(p_param_err,
11069  "longTermReferenceEnable is not supported on jpeg encoder",
11070  max_err_len);
11072  LRETURN;
11073  }
11074  if (p_cfg->ui32setLongTermInterval)
11075  {
11076  strncpy(p_param_err,
11077  "longTermReferenceInterval is not supported on jpeg encoder",
11078  max_err_len);
11080  LRETURN;
11081  }
11082  if (p_cfg->ui8setLongTermCount != 2)
11083  {
11084  strncpy(p_param_err,
11085  "longTermReferenceCount is not supported on jpeg encoder",
11086  max_err_len);
11088  LRETURN;
11089  }
11090  if (p_cfg->ui8multicoreJointMode)
11091  {
11092  strncpy(p_param_err,
11093  "multicoreJointMode is not supported on jpeg encoder",
11094  max_err_len);
11096  LRETURN;
11097  }
11098  if (p_cfg->ui8enableSSIM)
11099  {
11100  strncpy(p_param_err, "enableSSIM is not supported on jpeg encoder",
11101  max_err_len);
11103  LRETURN;
11104  }
11105  if (p_enc->rc.vbv_buffer_size != -1)
11106  {
11107  strncpy(p_param_err,
11108  "vbvBufferSize is not supported on jpeg encoder",
11109  max_err_len);
11111  LRETURN;
11112  }
11113  if (p_cfg->ui8fillerEnable)
11114  {
11115  strncpy(p_param_err,
11116  "fillerEnable is not supported on jpeg encoder",
11117  max_err_len);
11119  LRETURN;
11120  }
11121  if (p_cfg->ui8picSkipEnable)
11122  {
11123  strncpy(p_param_err, "picSkip is not supported on jpeg encoder",
11124  max_err_len);
11126  LRETURN;
11127  }
11128  if (p_cfg->ui16maxFrameSize)
11129  {
11130  strncpy(p_param_err, "maxFrameSize is not supported on jpeg encoder",
11131  max_err_len);
11133  LRETURN;
11134  }
11136  {
11137  strncpy(p_param_err,
11138  "cuLevelRCEnable is not supported on jpeg encoder",
11139  max_err_len);
11141  LRETURN;
11142  }
11143  if (p_cfg->niParamT408.enable_hvs_qp)
11144  {
11145  strncpy(p_param_err, "hvsQPEnable is not supported on jpeg encoder",
11146  max_err_len);
11148  LRETURN;
11149  }
11150  if (p_cfg->niParamT408.profile)
11151  {
11152  strncpy(p_param_err, "profile is not supported on jpeg encoder",
11153  max_err_len);
11155  LRETURN;
11156  }
11158  {
11159  strncpy(p_param_err, "intraRefreshMode or intraRefreshArg is not supported on jpeg encoder",
11160  max_err_len);
11162  LRETURN;
11163  }
11164  if (p_cfg->i8crf != -1)
11165  {
11166  strncpy(p_param_err, "crf is not supported on jpeg encoder",
11167  max_err_len);
11169  LRETURN;
11170  }
11171  if (p_cfg->i32tolCtbRcInter != (int32_t)(0.1 * 1000))
11172  {
11173  strncpy(p_param_err, "tolCtbRcInter is not supported on jpeg encoder",
11174  max_err_len);
11176  LRETURN;
11177  }
11178  if (p_cfg->i32tolCtbRcIntra != (int32_t)(0.1 * 1000))
11179  {
11180  strncpy(p_param_err, "tolCtbRcIntra is not supported on jpeg encoder",
11181  max_err_len);
11183  LRETURN;
11184  }
11185  if (p_cfg->ui8rcQpDeltaRange != 10)
11186  {
11187  strncpy(p_param_err,
11188  "rcQpDeltaRange is not supported on jpeg encoder",
11189  max_err_len);
11191  LRETURN;
11192  }
11193  if (p_cfg->i16bitrateWindow != -255)
11194  {
11195  strncpy(p_param_err, "bitrateWindow is not supported on jpeg encoder",
11196  max_err_len);
11198  LRETURN;
11199  }
11200  if (p_cfg->i16ctbRowQpStep)
11201  {
11202  strncpy(p_param_err, "ctbRowQpStep is not supported on jpeg encoder",
11203  max_err_len);
11205  LRETURN;
11206  }
11207  if (p_cfg->ui8AiEnhanceMode)
11208  {
11209  strncpy(p_param_err,
11210  "enableAIEnhance is not supported on jpeg encoder",
11211  max_err_len);
11213  LRETURN;
11214  }
11215  if (p_cfg->i8ppsInitQp)
11216  {
11217  strncpy(p_param_err, "ppsInitQp is not supported for jpeg encoder",
11218  max_err_len);
11220  LRETURN;
11221  }
11222  if (p_cfg->i8pass1Qp)
11223  {
11224  strncpy(p_param_err, "pass1Qp is not supported for jpeg encoder",
11225  max_err_len);
11227  LRETURN;
11228  }
11229  if (p_enc->bitrateMode != -1)
11230  {
11231  strncpy(p_param_err, "bitrateMode is not supported for jpeg encoder",
11232  max_err_len);
11234  LRETURN;
11235  }
11236  }
11237 
11238  if (p_src->force_frame_type != 0 && p_src->force_frame_type != 1)
11239  {
11240  strncpy(p_param_err, "Invalid forceFrameType: out of range",
11241  max_err_len);
11242  param_ret = NI_RETCODE_INVALID_PARAM;
11243  LRETURN;
11244  }
11245 
11246  if (p_cfg->niParamT408.forcedHeaderEnable > 2)
11247  {
11248  strncpy(p_param_err, "Invalid forcedHeaderEnable: out of range",
11249  max_err_len);
11250  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11251  LRETURN;
11252  }
11253 
11254  if (p_cfg->niParamT408.decoding_refresh_type < 0 ||
11256  {
11257  strncpy(p_param_err, "Invalid decoding_refresh_type: out of range", max_err_len);
11259  LRETURN;
11260  }
11261 
11262  if (!QUADRA)
11263  {
11264  if (p_cfg->niParamT408.gop_preset_index < 0 ||
11265  p_cfg->niParamT408.gop_preset_index > 8)
11266  {
11267  strcpy(p_param_err, "Invalid gop_preset_index: out of range");
11269  LRETURN;
11270  }
11271 
11272  if (p_src->low_delay_mode && 1 != p_cfg->niParamT408.gop_preset_index &&
11273  2 != p_cfg->niParamT408.gop_preset_index &&
11274  3 != p_cfg->niParamT408.gop_preset_index &&
11275  6 != p_cfg->niParamT408.gop_preset_index &&
11276  7 != p_cfg->niParamT408.gop_preset_index &&
11277  !(0 == p_cfg->niParamT408.gop_preset_index &&
11279  {
11280  strcpy(p_param_err, "GOP size must be 1 when lowDelay is enabled");
11282  LRETURN;
11283  }
11284  }
11285  else // QUADRA
11286  {
11287  if (p_cfg->ui8gopSize > 16)
11288  {
11289  strncpy(p_param_err, "Invalid gopSize out of range", max_err_len);
11291  LRETURN;
11292  }
11293 
11294  if (p_cfg->ui8gopLowdelay &&
11295  p_cfg->ui8gopSize > 4)
11296  {
11297  strncpy(p_param_err, "GOP size must be <= 4 for low delay GOP", max_err_len);
11299  LRETURN;
11300  }
11301 
11302  if (p_cfg->ui8LookAheadDepth)
11303  {
11304  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
11305  {
11306  strncpy(p_param_err, "Invalid LookAheadDepth: out of range. <[4-40]>", max_err_len);
11308  LRETURN;
11309  }
11310  if (p_cfg->ui8gopLowdelay)
11311  {
11312  strncpy(p_param_err, "2-pass encode does not support low delay GOP", max_err_len);
11314  LRETURN;
11315  }
11317  {
11318  strncpy(p_param_err, "2-pass encode does not support tile4x4 format",
11319  max_err_len);
11321  LRETURN;
11322  }
11323 
11324  }
11325 
11326  if (p_src->low_delay_mode || p_cfg->ui8picSkipEnable)
11327  {
11329  {
11330  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11331  {
11332  if (p_cfg->niParamT408.custom_gop_params.pic_param[i].poc_offset != (i+1))
11333  {
11334  if (p_src->low_delay_mode)
11335  strncpy(p_param_err, "Custom GOP must not include backward prediction when lowDelay is enabled", max_err_len);
11336  else
11337  strncpy(p_param_err, "Custom GOP must not include backward prediction when picSkip is enabled", max_err_len);
11338  param_ret = NI_RETCODE_INVALID_PARAM;
11339  LRETURN;
11340  }
11341  }
11342  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
11343  p_cfg->niParamT408.intra_period != 1 &&
11345  {
11346  if (p_src->low_delay_mode)
11347  strncpy(p_param_err, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when lowDelay is enabled", max_err_len);
11348  else
11349  strncpy(p_param_err, "Must use low delay GOP (gopPresetIdx 1,3,7,9,10) when picSkip is enabled", max_err_len);
11351  LRETURN;
11352  } else if ((p_cfg->ui8LookAheadDepth != 0) && (!p_cfg->ui8useLowDelayPocType))
11353  {
11354  if (p_src->low_delay_mode)
11355  strncpy(p_param_err, "lookAheadDepth must be 0 when lowDelay is enabled", max_err_len);
11356  else
11357  strncpy(p_param_err, "lookAheadDepth must be 0 when picSkip is enabled", max_err_len);
11358  param_ret = NI_RETCODE_INVALID_PARAM;
11359  LRETURN;
11360  }
11361 
11362  if (p_cfg->ui8multicoreJointMode)
11363  {
11364  if (p_src->low_delay_mode)
11365  strncpy(p_param_err,
11366  "Cannot use multicoreJointMode when lowDelay is enabled",
11367  max_err_len);
11368  else
11369  strncpy(p_param_err,
11370  "Cannot use multicoreJointMode when picSkip is enabled",
11371  max_err_len);
11372  param_ret = NI_RETCODE_INVALID_PARAM;
11373  LRETURN;
11374  }
11375 
11376  if (p_src->minFramesDelay)
11377  {
11378  if (p_src->low_delay_mode)
11379  strncpy(p_param_err,
11380  "Cannot enable minFramesDelay when lowDelay is enabled",
11381  max_err_len);
11382  else
11383  strncpy(p_param_err,
11384  "Cannot enable minFramesDelay when picSkip is enabled",
11385  max_err_len);
11386  param_ret = NI_RETCODE_INVALID_PARAM;
11387  LRETURN;
11388  }
11389  }
11390 
11391  if (p_cfg->ui8useLowDelayPocType)
11392  {
11394  {
11395  strncpy(p_param_err, "Custom GOP size must be 1 when useLowDelayPocType is enabled", max_err_len);
11396  param_ret = NI_RETCODE_INVALID_PARAM;
11397  LRETURN;
11398  } else if (1 != p_cfg->ui8gopSize && !p_cfg->ui8gopLowdelay &&
11399  p_cfg->niParamT408.intra_period != 1)
11400  {
11401  strncpy(p_param_err, "Must use GOP with all frames as reference frames (gopPresetIdx 1,3,7,9) when useLowDelayPocType is enabled", max_err_len);
11403  LRETURN;
11404  }
11405  }
11406 
11407  if (p_src->low_delay_mode)
11408  {
11409  // minimum acceptable value of maxFrameSize is bitrate / framerate in bytes
11410  uint32_t min_maxFrameSize = p_cfg->i32bitRate / p_cfg->i32frameRateInfo * p_cfg->i32frameRateDenominator / 8;
11411 
11412  if (p_cfg->ui16maxFrameSize == 0)
11413  {
11414  if (p_enc->maxFrameSizeRatio > 0)
11415  {
11416  if (min_maxFrameSize * p_enc->maxFrameSizeRatio > NI_MAX_FRAME_SIZE)
11417  {
11418  p_cfg->ui16maxFrameSize = NI_MAX_FRAME_SIZE / 2000;
11419  } else
11420  {
11421  p_cfg->ui16maxFrameSize = min_maxFrameSize * p_enc->maxFrameSizeRatio / 2000;
11422  }
11423  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: Set maxFrameSize to %d times the "
11424  "minimum frame size %d bytes in low delay mode\n", __func__,
11425  p_enc->maxFrameSizeRatio, p_cfg->ui16maxFrameSize * 2000);
11426  } else
11427  {
11428  p_cfg->ui16maxFrameSize = ((p_src->source_width * p_src->source_height * 3 / 4) * p_ctx->bit_depth_factor) / 2000;
11429  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize is not set in low delay "
11430  "mode. Set it to half of the maximum frame size %d bytes\n",
11431  __func__, p_cfg->ui16maxFrameSize*2000);
11432  }
11433  }
11434 
11435  if (p_cfg->ui16maxFrameSize < min_maxFrameSize / 2000)
11436  {
11437  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: maxFrameSize %u is too small. Changed to minimum value (bitrate/framerate in byte): %u\n",
11438  __func__, p_cfg->ui16maxFrameSize*2000, min_maxFrameSize);
11439  p_cfg->ui16maxFrameSize = min_maxFrameSize / 2000;
11440  }
11441  }
11442  else
11443  {
11444  if (p_cfg->ui16maxFrameSize != 0 || p_enc->maxFrameSizeRatio > 0)
11445  {
11446  strncpy(p_param_err, "maxFrameSize can only be used when lowDelay is enabled", max_err_len);
11447  param_ret = NI_RETCODE_INVALID_PARAM;
11448  LRETURN;
11449  }
11450  }
11451  }
11452 
11453  if (QUADRA)
11454  {
11455  if (p_cfg->ui16gdrDuration)
11456  {
11458  {
11459  for (i = 0; i < p_cfg->niParamT408.custom_gop_params.custom_gop_size; i++)
11460  {
11461  if (2 == p_cfg->niParamT408.custom_gop_params.pic_param[i].pic_type)
11462  {
11463  strncpy(p_param_err, "Custom GOP can not have B frames for intra refresh", max_err_len);
11464  param_ret = NI_RETCODE_INVALID_PARAM;
11465  LRETURN;
11466  }
11467  }
11468  }
11469  else if (p_cfg->ui8gopSize != 1 || p_cfg->ui8gopLowdelay)
11470  {
11472  {
11473  strncpy(p_param_err,
11474  "Must use gopPresetIdx 9 (consecutive P frame) for intra refresh",
11475  max_err_len);
11476  param_ret = NI_RETCODE_INVALID_PARAM;
11477  LRETURN;
11478  }
11479  strncpy(p_param_warn, "GOP size forced to 1 and low delay GOP force disabled (no B frames) for intra refresh", max_err_len);
11480  warning = NI_RETCODE_PARAM_WARN;
11481  p_cfg->ui8gopSize = 1;
11482  p_cfg->ui8gopLowdelay = 0;
11483  }
11484  if (p_cfg->ui16gdrDuration == 1)
11485  {
11486  strncpy(p_param_err,
11487  "intra refresh cycle (height / intraRefreshArg MB or CTU) must > 1",
11488  max_err_len);
11489  param_ret = NI_RETCODE_INVALID_PARAM;
11490  LRETURN;
11491  }
11492  if (p_cfg->ui8LookAheadDepth != 0)
11493  {
11494  strncpy(p_param_err, "lookaheadDepth must be 0 for intra refresh", max_err_len);
11495  param_ret = NI_RETCODE_INVALID_PARAM;
11496  LRETURN;
11497  }
11498  if (STD_HEVC == p_cfg->ui8bitstreamFormat ||
11499  STD_AV1 == p_cfg->ui8bitstreamFormat)
11500  {
11501  if (p_cfg->niParamT408.intra_period < p_cfg->ui16gdrDuration)
11502  {
11503  strncpy(p_param_warn, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len);
11504  p_cfg->niParamT408.intra_period = p_cfg->ui16gdrDuration;
11505  }
11506  }
11507  else if (STD_AVC == p_cfg->ui8bitstreamFormat)
11508  {
11509  if (p_cfg->niParamT408.avcIdrPeriod < p_cfg->ui16gdrDuration)
11510  {
11511  strncpy(p_param_warn, "intraPeriod forced to match intra refersh cycle (intraPeriod must >= intra refersh cycle)", max_err_len);
11512  p_cfg->niParamT408.avcIdrPeriod = p_cfg->ui16gdrDuration;
11513  }
11514  }
11515  }
11516 
11517  if ((p_cfg->ui8hrdEnable) || (p_cfg->ui8fillerEnable))
11518  {
11519  // enable rate control
11520  if (p_cfg->ui8rcEnable == 0)
11521  {
11522  p_cfg->ui8rcEnable = p_src->cfg_enc_params.rc.enable_rate_control = 1;
11523  }
11524 
11525  // enable hrd if it is off
11526  if (p_cfg->i32vbvBufferSize == 0)
11527  {
11528  p_cfg->i32vbvBufferSize = 3000;
11529  }
11530  }
11531 
11532  // maxrate must >= bitrate
11533  if (p_cfg->ui32vbvMaxRate != 0)
11534  {
11535  if (p_cfg->ui32vbvMaxRate < p_cfg->i32bitRate)
11536  {
11537  snprintf(p_param_err, max_err_len, "vbvMaxRate %u cannot be smaller than bitrate %d",
11538  p_cfg->ui32vbvMaxRate, p_cfg->i32bitRate);
11539  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11540  LRETURN;
11541  }
11542  }
11543 
11544  // minrate must <= bitrate
11545  if (p_cfg->ui32vbvMinRate != 0)
11546  {
11547  if (p_cfg->ui32vbvMinRate > p_cfg->i32bitRate)
11548  {
11549  snprintf(p_param_err, max_err_len, "vbvMinRate %u cannot be larger than bitrate %d",
11550  p_cfg->ui32vbvMinRate, p_cfg->i32bitRate);
11551  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11552  LRETURN;
11553  }
11554  }
11555 
11556  // The supported range of vbvBufferSize are 0, or 1/framerate to 3000. If the vbvBufferSize is not 0.
11557  // The minimum vbvBufferSize in msec is 1/framerate. The actual minimum in bits is bitrate/framerate.
11558  if (p_cfg->i32vbvBufferSize != 0)
11559  {
11560  // check buffer size compatible to bitrate
11561  uint32_t minVbvBufferSize = p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo;
11562  if (p_cfg->i32vbvBufferSize < minVbvBufferSize)
11563  {
11564  snprintf(p_param_err, max_err_len, "vbvBufferSize must be greater than the average frame size. Minimum is %u msec for framerate %d fps",
11565  minVbvBufferSize, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
11566  param_ret = NI_RETCODE_PARAM_INVALID_VALUE;
11567  LRETURN;
11568  }
11569 
11570  // check buffer size compatible to maxrate
11571  if (p_cfg->ui32vbvMaxRate != 0)
11572  {
11573  uint32_t maxRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMaxRate / p_cfg->i32bitRate;
11574  if (p_cfg->i32vbvBufferSize < maxRateMinVbvBufferSize)
11575  {
11576  snprintf(p_param_warn, max_err_len, "vbvBufferSize cannot be smaller than one frame size based on vbvMaxRate, force vbvBufferSize to %u msec for bitrate %d vbvMaxRate %u and framerate %d fps",
11577  maxRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMaxRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
11578  warning = NI_RETCODE_PARAM_WARN;
11579  p_cfg->i32vbvBufferSize = maxRateMinVbvBufferSize;
11580  }
11581  }
11582  // check buffer size compatible to minrate (capped CRF may set minrate without maxrate)
11583  else if (p_cfg->ui32vbvMinRate != 0)
11584  {
11585  uint32_t minRateMinVbvBufferSize = ((int64_t)p_cfg->i32frameRateDenominator * 1000 / p_cfg->i32frameRateInfo) * p_cfg->ui32vbvMinRate / p_cfg->i32bitRate;
11586  if (p_cfg->i32vbvBufferSize < minRateMinVbvBufferSize)
11587  {
11588  snprintf(p_param_warn, max_err_len, "vbvBufferSize cannot be smaller than one frame size based on vbvMinRate, force vbvBufferSize to %u msec for bitrate %d vbvMinRate %u and framerate %d fps",
11589  minRateMinVbvBufferSize, p_cfg->i32bitRate, p_cfg->ui32vbvMinRate, (p_cfg->i32frameRateInfo / p_cfg->i32frameRateDenominator));
11590  warning = NI_RETCODE_PARAM_WARN;
11591  p_cfg->i32vbvBufferSize = minRateMinVbvBufferSize;
11592  }
11593  }
11594  }
11595  else
11596  {
11597  // check buffer size compatible to maxrate and/or minrate
11598  if ( p_cfg->ui32vbvMaxRate != 0 || p_cfg->ui32vbvMinRate != 0 )
11599  {
11600  snprintf(p_param_warn, max_err_len, "vbvMaxRate %u vbvMinRate %u does not take effect when vbvBufferSize is 0, force vbvMaxRate vbvMinRate to 0",
11601  p_cfg->ui32vbvMaxRate, p_cfg->ui32vbvMinRate);
11602  warning = NI_RETCODE_PARAM_WARN;
11603  p_cfg->ui32vbvMaxRate = 0;
11604  p_cfg->ui32vbvMinRate = 0;
11605  }
11606  }
11607 
11608  if (p_cfg->ui32ltrRefInterval || p_cfg->niParamT408.useLongTerm)
11609  {
11610  if (p_cfg->ui32ltrRefInterval && p_cfg->niParamT408.useLongTerm)
11611  {
11612  strncpy(p_param_err,
11613  "Can't enable ltrRefInterval and longTermReferenceEnable "
11614  "at same time",
11615  max_err_len);
11616  param_ret = NI_RETCODE_INVALID_PARAM;
11617  LRETURN;
11618  }
11619 
11621  {
11623  {
11624  strncpy(p_param_err, "Custom GOP size can not be > 1 for long term reference", max_err_len);
11625  param_ret = NI_RETCODE_INVALID_PARAM;
11626  LRETURN;
11627  }
11628  } else if ((p_cfg->ui8gopSize != 1) && (p_cfg->ui8gopLowdelay == 0) &&
11629  (p_cfg->niParamT408.intra_period != 1))
11630  {
11632  {
11633  strncpy(p_param_err,
11634  "Must use low delay GOP (gopPresetIdx 1,3,7,9) for long term reference",
11635  max_err_len);
11636  param_ret = NI_RETCODE_INVALID_PARAM;
11637  LRETURN;
11638  }
11639  strncpy(p_param_warn, "GOP size forced to 1 for long term reference", max_err_len);
11640  warning = NI_RETCODE_PARAM_WARN;
11641  p_cfg->ui8gopSize = 1;
11642  }
11643 
11644  if (p_cfg->ui8LookAheadDepth != 0)
11645  {
11646  strncpy(p_param_err, "lookaheadDepth must be 0 for long term reference", max_err_len);
11647  param_ret = NI_RETCODE_INVALID_PARAM;
11648  LRETURN;
11649  }
11650  }
11651 
11652  if (p_cfg->ui32setLongTermInterval && (p_cfg->niParamT408.useLongTerm == 0))
11653  {
11654  strncpy(
11655  p_param_err,
11656  "Must set longTermReferenceEnable for longTermReferenceInterval",
11657  max_err_len);
11658  param_ret = NI_RETCODE_INVALID_PARAM;
11659  LRETURN;
11660  }
11661 
11662  if (p_cfg->ui8av1ErrResilientMode)
11663  {
11664  if (STD_AV1 != p_cfg->ui8bitstreamFormat)
11665  {
11666  strncpy(p_param_warn, "AV1 err resilient mode forced to 0 when using other codecs", max_err_len);
11667  warning = NI_RETCODE_PARAM_WARN;
11668  p_cfg->ui8av1ErrResilientMode = 0;
11669  }
11670  }
11671  }
11672 
11673  if (p_cfg->niParamT408.cu_size_mode < 0 ||
11674  p_cfg->niParamT408.cu_size_mode > 7)
11675  {
11676  strncpy(p_param_err, "Invalid cu_size_mode: out of range", max_err_len);
11678  LRETURN;
11679  }
11680 
11681 
11682 
11683  if (p_cfg->niParamT408.use_recommend_enc_params < 0 ||
11685  {
11686  strncpy(p_param_err, "Invalid use_recommend_enc_params: out of range", max_err_len);
11688  LRETURN;
11689  }
11690 
11691  switch (p_cfg->niParamT408.use_recommend_enc_params)
11692  {
11693  case 0:
11694  case 2:
11695  case 3:
11696  {
11697  if (p_cfg->niParamT408.use_recommend_enc_params != 3)
11698  {
11699  // in FAST mode (recommendEncParam==3), max_num_merge value will be
11700  // decided in FW
11701  if (p_cfg->niParamT408.max_num_merge < 0 ||
11702  p_cfg->niParamT408.max_num_merge > 3)
11703  {
11704  strncpy(p_param_err, "Invalid max_num_merge: out of range", max_err_len);
11706  LRETURN;
11707  }
11708  }
11709  break;
11710  }
11711 
11712  default: break;
11713  }
11714 
11715  if ( p_cfg->niParamT408.intra_qp < -1 ||
11716  p_cfg->niParamT408.intra_qp > 51 )
11717  {
11718  strncpy(p_param_err, "Invalid intra_qp: out of range", max_err_len);
11719  param_ret = NI_RETCODE_PARAM_ERROR_INTRA_QP;
11720  LRETURN;
11721  }
11722 
11723  if (QUADRA)
11724  {
11725  if (p_cfg->i8crf >= 0 && p_cfg->i8crf <= 51)
11726  {
11727  if (p_cfg->ui8LookAheadDepth < 4 || p_cfg->ui8LookAheadDepth > 40)
11728  {
11730  "6X") < 0)
11731  {
11732  strncpy(p_param_err, "CRF requres LookAheadDepth <[4-40]>", max_err_len);
11734  LRETURN;
11735  }
11736  else
11737  {
11738  p_cfg->ui8LookAheadDepth = 1;
11739  p_cfg->ui8noMbtree = 1;
11740  strncpy(p_param_warn, "enable lookahead of current frame", max_err_len);
11741  warning = NI_RETCODE_PARAM_WARN;
11742  }
11743  }
11744 
11745  if (p_cfg->ui8rcEnable == 1)
11746  {
11747  strncpy(p_param_err, "CRF requires RcEnable 0", max_err_len);
11748  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
11749  LRETURN;
11750  }
11751  #if 0
11752  if (p_cfg->ui8ctbRcMode > 0)
11753  {
11754  strncpy(p_param_warn, "Lookahead with cuLevelRCEnable or hvsQPEnable may degrade quality", max_err_len);
11755  warning = NI_RETCODE_PARAM_WARN;
11756  //LRETURN;
11757  }
11758  #endif
11759  }
11760 
11762  {
11763  if (p_cfg->ui8LookAheadDepth == 0)
11764  {
11765  strncpy(p_param_err, "tuneBframeVisual level 1 (medium) requires lookahead or crf encode", max_err_len);
11766  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
11767  LRETURN;
11768  }
11769  }
11770  }
11771 
11772  if ( p_cfg->niParamT408.enable_mb_level_rc != 1 &&
11773  p_cfg->niParamT408.enable_mb_level_rc != 0 )
11774  {
11775  strncpy(p_param_err, "Invalid enable_mb_level_rc: out of range", max_err_len);
11776  param_ret = NI_RETCODE_PARAM_ERROR_RCENABLE;
11777  LRETURN;
11778  }
11779 
11780  {
11781  if ( p_cfg->niParamT408.minQpI < 0 ||
11782  p_cfg->niParamT408.minQpI > 51 )
11783  {
11784  strncpy(p_param_err, "Invalid min_qp: out of range", max_err_len);
11785  param_ret = NI_RETCODE_PARAM_ERROR_MN_QP;
11786  LRETURN;
11787  }
11788 
11789  if ( p_cfg->niParamT408.maxQpI < 0 ||
11790  p_cfg->niParamT408.maxQpI > 51 )
11791  {
11792  strncpy(p_param_err, "Invalid max_qp: out of range", max_err_len);
11793  param_ret = NI_RETCODE_PARAM_ERROR_MX_QP;
11794  LRETURN;
11795  }
11796 
11797  if ( p_cfg->niParamT408.enable_cu_level_rate_control != 1 &&
11799  {
11800  strncpy(p_param_err, "Invalid enable_cu_level_rate_control: out of range", max_err_len);
11802  LRETURN;
11803  }
11804 
11805  //if (p_cfg->niParamT408.enable_cu_level_rate_control == 1)
11806  {
11807  if ( p_cfg->niParamT408.enable_hvs_qp != 1 &&
11808  p_cfg->niParamT408.enable_hvs_qp != 0 )
11809  {
11810  strncpy(p_param_err, "Invalid enable_hvs_qp: out of range", max_err_len);
11812  LRETURN;
11813  }
11814 
11815  if (p_cfg->niParamT408.enable_hvs_qp)
11816  {
11817  if ( p_cfg->niParamT408.max_delta_qp < 0 ||
11818  p_cfg->niParamT408.max_delta_qp > 51 )
11819  {
11820  strncpy(p_param_err, "Invalid max_delta_qp: out of range", max_err_len);
11822  LRETURN;
11823  }
11824  }
11825  }
11826  // hrd is off when i32vbvBufferSize is 0
11827  if ((p_cfg->i32vbvBufferSize < 10 && p_cfg->i32vbvBufferSize != 0) || p_cfg->i32vbvBufferSize > 3000)
11828  {
11829  strncpy(p_param_err, "Invalid i32vbvBufferSize: out of range", max_err_len);
11831  LRETURN;
11832  }
11833  }
11834 
11835  // check valid for common param
11836  param_ret = ni_check_common_params(&p_cfg->niParamT408, p_src, p_param_err, max_err_len);
11837  if (param_ret != NI_RETCODE_SUCCESS)
11838  {
11839  LRETURN;
11840  }
11841 
11842  // check valid for RC param
11843  param_ret = ni_check_ratecontrol_params(p_cfg, p_param_err, max_err_len);
11844  if (param_ret != NI_RETCODE_SUCCESS)
11845  {
11846  LRETURN;
11847  }
11848 
11850  {
11851  p_ctx->keyframe_factor =
11852  presetGopKeyFrameFactor[p_cfg->niParamT408.gop_preset_index];
11853  }
11854  if (warning == NI_RETCODE_PARAM_WARN && param_ret == NI_RETCODE_SUCCESS)
11855  {
11856  param_ret = NI_RETCODE_PARAM_WARN;
11857  strncpy(p_param_err, p_param_warn, max_err_len);
11858  }
11859 
11860 END:
11861  free(p_param_warn);
11862  return param_ret;
11863 }
11864 
11866  ni_xcoder_params_t *p_src,
11867  char *p_param_err, uint32_t max_err_len)
11868 {
11870  int32_t low_delay = 0;
11871  int32_t intra_period_gop_step_size;
11872  int32_t i, j;
11873 
11874  if (!p_param || !p_src || !p_param_err)
11875  {
11876  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
11877  __func__);
11879  LRETURN;
11880  }
11881 
11882  //Zero out the error buffer
11883  memset(p_param_err, 0, max_err_len);
11884 
11885  // check low-delay gop structure
11886  if (!QUADRA)
11887  {
11888  if (0 == p_param->gop_preset_index) // common gop
11889  {
11890  if (p_param->custom_gop_params.custom_gop_size > 1)
11891  {
11892  int minVal = p_param->custom_gop_params.pic_param[0].poc_offset;
11893  low_delay = 1;
11894  for (i = 1; i < p_param->custom_gop_params.custom_gop_size; i++)
11895  {
11896  if (minVal > p_param->custom_gop_params.pic_param[i].poc_offset)
11897  {
11898  low_delay = 0;
11899  break;
11900  } else
11901  {
11902  minVal = p_param->custom_gop_params.pic_param[i].poc_offset;
11903  }
11904  }
11905  }
11906  }
11907  else if (p_param->gop_preset_index == 2 ||
11908  p_param->gop_preset_index == 3 ||
11909  p_param->gop_preset_index == 6 ||
11910  p_param->gop_preset_index == 7) // low-delay case (IPPP, IBBB)
11911  {
11912  low_delay = 1;
11913  }
11914 
11915  if (low_delay)
11916  {
11917  intra_period_gop_step_size = 1;
11918  }
11919  else
11920  {
11921  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
11922  {
11923  intra_period_gop_step_size = p_param->custom_gop_params.custom_gop_size;
11924  }
11925  else
11926  {
11927  intra_period_gop_step_size = presetGopSize[p_param->gop_preset_index];
11928  }
11929  }
11930 
11931  if (((p_param->intra_period != 0) && ((p_param->intra_period < intra_period_gop_step_size+1) == 1)) ||
11932  ((p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod < intra_period_gop_step_size+1) == 1)))
11933  {
11934  strncpy(p_param_err, "Invalid intra_period and gop_preset_index: gop structure is larger than intra period", max_err_len);
11936  LRETURN;
11937  }
11938 
11939  if (((!low_delay) && (p_param->intra_period != 0) && ((p_param->intra_period % intra_period_gop_step_size) != 0)) ||
11940  ((!low_delay) && (p_param->avcIdrPeriod != 0) && ((p_param->avcIdrPeriod % intra_period_gop_step_size) != 0)))
11941  {
11942  strncpy(p_param_err, "Invalid intra_period and gop_preset_index: intra period is not a multiple of gop structure size", max_err_len);
11944  LRETURN;
11945  }
11946 
11947  if (p_param->gop_preset_index == GOP_PRESET_IDX_CUSTOM)
11948  {
11949  int temp_poc[NI_MAX_GOP_NUM];
11950  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
11951  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
11952  {
11954  {
11955  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len);
11957  LRETURN;
11958  }
11959 
11960  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
11961  {
11962  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id is zero or negative", max_err_len);
11964  LRETURN;
11965  }
11966  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
11967  if (min_poc > temp_poc[i])
11968  {
11969  min_poc = temp_poc[i];
11970  }
11971  }
11972  int count_pos = 0;
11973  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
11974  {
11975  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
11976  {
11977  if (temp_poc[j] == min_poc)
11978  {
11979  count_pos++;
11980  min_poc++;
11981  }
11982  }
11983  }
11984  if (count_pos != p_param->custom_gop_params.custom_gop_size)
11985  {
11986  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset is invalid", max_err_len);
11988  LRETURN;
11989  }
11990  }
11991  }
11992  else // QUADRA
11993  {
11994  if (p_param->custom_gop_params.custom_gop_size)
11995  {
11996  int temp_poc[NI_MAX_GOP_NUM];
11997  int min_poc = p_param->custom_gop_params.pic_param[0].poc_offset;
11998  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
11999  {
12001  {
12002  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id larger than 7", max_err_len);
12004  LRETURN;
12005  }
12006 
12007  if (p_param->custom_gop_params.pic_param[i].temporal_id < 0)
12008  {
12009  strncpy(p_param_err, "Invalid custom gop parameters: temporal_id is negative", max_err_len);
12011  LRETURN;
12012  }
12013 
12014  for (j = 0; j < p_param->custom_gop_params.pic_param[i].num_ref_pics; j++)
12015  {
12016  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic == 0)
12017  {
12018  strncpy(p_param_err, "Invalid custom gop parameters: ref pic delta cannot be 0", max_err_len);
12020  LRETURN;
12021  }
12022  }
12023 
12024  for (j = 0; j < NI_MAX_REF_PIC; j++)
12025  {
12026  if (p_param->custom_gop_params.pic_param[i].rps[j].ref_pic != 0 &&
12027  p_param->custom_gop_params.pic_param[i].rps[j].ref_pic_used == -1)
12028  {
12029  ni_log(NI_LOG_ERROR,"g%drefPic%d specified without g%drefPic%dUsed specified!\n", i, j, i, j);
12031  LRETURN;
12032  }
12033  }
12034 
12035  temp_poc[i] = p_param->custom_gop_params.pic_param[i].poc_offset;
12036  if (min_poc > temp_poc[i])
12037  {
12038  min_poc = temp_poc[i];
12039  }
12040  }
12041  int count_pos = 0;
12042  for (i = 0; i < p_param->custom_gop_params.custom_gop_size; i++)
12043  {
12044  for (j = 0; j < p_param->custom_gop_params.custom_gop_size; j++)
12045  {
12046  if (temp_poc[j] == min_poc)
12047  {
12048  count_pos++;
12049  min_poc++;
12050  }
12051  }
12052  }
12053  if (count_pos != p_param->custom_gop_params.custom_gop_size)
12054  {
12055  strncpy(p_param_err, "Invalid custom gop parameters: poc_offset is invalid", max_err_len);
12057  LRETURN;
12058  }
12059  }
12060  }
12061 
12062  if (0 == p_param->use_recommend_enc_params)
12063  {
12064  // RDO
12065  {
12066  int align_32_width_flag = p_src->source_width % 32;
12067  int align_16_width_flag = p_src->source_width % 16;
12068  int align_8_width_flag = p_src->source_width % 8;
12069  int align_32_height_flag = p_src->source_height % 32;
12070  int align_16_height_flag = p_src->source_height % 16;
12071  int align_8_height_flag = p_src->source_height % 8;
12072 
12073  if (((p_param->cu_size_mode & 0x1) == 0) && ((align_8_width_flag != 0) || (align_8_height_flag != 0)))
12074  {
12075  strncpy(p_param_err, "Invalid use_recommend_enc_params and cu_size_mode: picture width and height must be aligned with 8 pixels when enable CU8x8 of cu_size_mode. Recommend to set cu_size_mode |= 0x1 (CU8x8)", max_err_len);
12077  LRETURN;
12078  }
12079  else if (((p_param->cu_size_mode & 0x1) == 0) && ((p_param->cu_size_mode & 0x2) == 0) && ((align_16_width_flag != 0) || (align_16_height_flag != 0)))
12080  {
12081  strncpy(p_param_err, "Invalid use_recommend_enc_params and cu_size_mode: picture width and height must be aligned with 16 pixels when enable CU16x16 of cu_size_mode. Recommend to set cu_size_mode |= 0x2 (CU16x16)", max_err_len);
12083  LRETURN;
12084  }
12085  else if (((p_param->cu_size_mode & 0x1) == 0) && ((p_param->cu_size_mode & 0x2) == 0) && ((p_param->cu_size_mode & 0x4) == 0) && ((align_32_width_flag != 0) || (align_32_height_flag != 0)))
12086  {
12087  strncpy(p_param_err, "Invalid use_recommend_enc_params and cu_size_mode: picture width and height must be aligned with 32 pixels when enable CU32x32 of cu_size_mode. Recommend to set cu_size_mode |= 0x4 (CU32x32)", max_err_len);
12089  LRETURN;
12090  }
12091  }
12092  }
12093 
12094  if ((p_param->conf_win_top < 0) || (p_param->conf_win_top > 8192))
12095  {
12096  strncpy(p_param_err, "Invalid conf_win_top: out of range", max_err_len);
12098  LRETURN;
12099  }
12100  if (p_param->conf_win_top % 2)
12101  {
12102  strncpy(p_param_err, "Invalid conf_win_top: not multiple of 2", max_err_len);
12104  LRETURN;
12105  }
12106 
12107  if ((p_param->conf_win_bottom < 0) || (p_param->conf_win_bottom > 8192))
12108  {
12109  strncpy(p_param_err, "Invalid conf_win_bottom: out of range", max_err_len);
12111  LRETURN;
12112  }
12113  if (p_param->conf_win_bottom % 2)
12114  {
12115  strncpy(p_param_err, "Invalid conf_win_bottom: not multiple of 2", max_err_len);
12117  LRETURN;
12118  }
12119 
12120  if ((p_param->conf_win_left < 0) || (p_param->conf_win_left > 8192))
12121  {
12122  strncpy(p_param_err, "Invalid conf_win_left: out of range", max_err_len);
12124  LRETURN;
12125  }
12126  if (p_param->conf_win_left % 2)
12127  {
12128  strncpy(p_param_err, "Invalid conf_win_left: not multiple of 2", max_err_len);
12130  LRETURN;
12131  }
12132 
12133  if (p_param->conf_win_right < 0 || p_param->conf_win_right > 8192)
12134  {
12135  strncpy(p_param_err, "Invalid conf_win_right: out of range", max_err_len);
12137  LRETURN;
12138  }
12139  if (p_param->conf_win_right % 2)
12140  {
12141  strncpy(p_param_err, "Invalid conf_win_right: not multiple of 2", max_err_len);
12143  }
12144 
12145 END:
12146 
12147  return ret;
12148 }
12149 
12150 ni_retcode_t ni_check_ratecontrol_params(ni_encoder_config_t* p_cfg, char* p_param_err, uint32_t max_err_len)
12151 {
12153  ni_t408_config_t* p_param = NULL;
12154 
12155  if( (!p_cfg) || (!p_param_err) )
12156  {
12157  ni_log(NI_LOG_ERROR, "ERROR: %s() Null pointer parameters passed\n",
12158  __func__);
12160  LRETURN;
12161  }
12162  p_param = &p_cfg->niParamT408;
12163 
12164  //Zero out the error buffer
12165  memset(p_param_err, 0, max_err_len);
12166 
12167  if (p_param->roiEnable != 0 && p_param->roiEnable != 1)
12168  {
12169  strncpy(p_param_err, "Invalid roiEnable: out of range", max_err_len);
12171  LRETURN;
12172  }
12173 
12174  if (p_param->roiEnable && p_param->enable_hvs_qp)
12175  {
12176  strncpy(p_param_err, "hvsQPEnable and roiEnable: not mutually exclusive", max_err_len);
12178  LRETURN;
12179  }
12180 
12181  if (p_cfg->ui8rcEnable == 1)
12182  {
12183  if (p_param->minQpP > p_param->maxQpP || p_param->minQpB > p_param->maxQpB)
12184  {
12185  strncpy(p_param_err, "Invalid min_qp(P/B) and max_qp(P/B): min_qp cannot be larger than max_qp", max_err_len);
12187  LRETURN;
12188  }
12189  }
12190 
12191 END:
12192 
12193  return ret;
12194 }
12195 
12196 
12197 /*!******************************************************************************
12198  * \brief Print xcoder user configurations
12199  *
12200  * \param
12201  *
12202  * \return
12203  *******************************************************************************/
12204 void ni_params_print(ni_xcoder_params_t *const p_encoder_params)
12205 {
12206  if (!p_encoder_params)
12207  {
12208  return;
12209  }
12210 
12211  ni_encoder_cfg_params_t *p_enc = &p_encoder_params->cfg_enc_params;
12212 
12213  ni_log(NI_LOG_DEBUG, "XCoder Params:\n");
12214 
12215  ni_log(NI_LOG_DEBUG, "preset=%d\n", p_encoder_params->preset);
12216  ni_log(NI_LOG_DEBUG, "fps_number / fps_denominator=%u / %u\n",
12217  p_encoder_params->fps_number,
12218  p_encoder_params->fps_denominator);
12219 
12220  ni_log(NI_LOG_DEBUG, "source_width x source_height=%dx%d\n", p_encoder_params->source_width, p_encoder_params->source_height);
12221  ni_log(NI_LOG_DEBUG, "bitrate=%d\n", p_encoder_params->bitrate);
12222 
12223  ni_log(NI_LOG_DEBUG, "profile=%d\n", p_enc->profile);
12224  ni_log(NI_LOG_DEBUG, "level_idc=%d\n", p_enc->level_idc);
12225  ni_log(NI_LOG_DEBUG, "high_tier=%d\n", p_enc->high_tier);
12226 
12227  ni_log(NI_LOG_DEBUG, "frame_rate=%d\n", p_enc->frame_rate);
12228 
12229  ni_log(NI_LOG_DEBUG, "use_recommend_enc_params=%d\n", p_enc->use_recommend_enc_params);
12230  ni_log(NI_LOG_DEBUG, "cu_size_mode=%d\n", p_enc->cu_size_mode);
12231  ni_log(NI_LOG_DEBUG, "max_num_merge=%d\n", p_enc->max_num_merge);
12232  ni_log(NI_LOG_DEBUG, "enable_dynamic_8x8_merge=%d\n", p_enc->enable_dynamic_8x8_merge);
12233  ni_log(NI_LOG_DEBUG, "enable_dynamic_16x16_merge=%d\n", p_enc->enable_dynamic_16x16_merge);
12234  ni_log(NI_LOG_DEBUG, "enable_dynamic_32x32_merge=%d\n", p_enc->enable_dynamic_32x32_merge);
12235  // trans_rate not available in Rev B
12236  ni_log(NI_LOG_DEBUG, "enable_rate_control=%d\n", p_enc->rc.enable_rate_control);
12237  ni_log(NI_LOG_DEBUG, "enable_cu_level_rate_control=%d\n", p_enc->rc.enable_cu_level_rate_control);
12238  ni_log(NI_LOG_DEBUG, "enable_hvs_qp=%d\n", p_enc->rc.enable_hvs_qp);
12239  ni_log(NI_LOG_DEBUG, "enable_hvs_qp_scale=%d\n", p_enc->rc.enable_hvs_qp_scale);
12240  ni_log(NI_LOG_DEBUG, "hvs_qp_scale=%d\n", p_enc->rc.hvs_qp_scale);
12241  ni_log(NI_LOG_DEBUG, "min_qp=%d\n", p_enc->rc.min_qp);
12242  ni_log(NI_LOG_DEBUG, "max_qp=%d\n", p_enc->rc.max_qp);
12243  ni_log(NI_LOG_DEBUG, "max_delta_qp=%d\n", p_enc->rc.max_delta_qp);
12244  ni_log(NI_LOG_DEBUG, "vbv_buffer_size=%d\n", p_enc->rc.vbv_buffer_size);
12245  ni_log(NI_LOG_DEBUG, "enable_filler=%d\n", p_enc->rc.enable_filler);
12246  ni_log(NI_LOG_DEBUG, "enable_pic_skip=%d\n", p_enc->rc.enable_pic_skip);
12247 
12248  ni_log(NI_LOG_DEBUG, "forcedHeaderEnable=%d\n", p_enc->forced_header_enable);
12249  ni_log(NI_LOG_DEBUG, "roi_enable=%d\n", p_enc->roi_enable);
12250  ni_log(NI_LOG_DEBUG, "long_term_ref_enable=%d\n", p_enc->long_term_ref_enable);
12251  ni_log(NI_LOG_DEBUG, "long_term_ref_interval=%d\n", p_enc->long_term_ref_interval);
12252  ni_log(NI_LOG_DEBUG, "long_term_ref_count=%d\n", p_enc->long_term_ref_count);
12253  ni_log(NI_LOG_DEBUG, "conf_win_top=%d\n", p_enc->conf_win_top);
12254  ni_log(NI_LOG_DEBUG, "conf_win_bottom=%d\n", p_enc->conf_win_bottom);
12255  ni_log(NI_LOG_DEBUG, "conf_win_left=%d\n", p_enc->conf_win_left);
12256  ni_log(NI_LOG_DEBUG, "conf_win_right=%d\n", p_enc->conf_win_right);
12257 
12258  ni_log(NI_LOG_DEBUG, "intra_qp=%d\n", p_enc->rc.intra_qp);
12259  ni_log(NI_LOG_DEBUG, "enable_mb_level_rc=%d\n", p_enc->rc.enable_mb_level_rc);
12260 
12261  ni_log(NI_LOG_DEBUG, "intra_period=%d\n", p_enc->intra_period);
12262  ni_log(NI_LOG_DEBUG, "decoding_refresh_type=%d\n", p_enc->decoding_refresh_type);
12263 
12264  // Rev. B: H.264 only or HEVC-shared parameters, in ni_t408_config_t
12265  ni_log(NI_LOG_DEBUG, "enable_transform_8x8=%d\n", p_enc->enable_transform_8x8);
12266  ni_log(NI_LOG_DEBUG, "slice_mode=%d\n", p_enc->slice_mode);
12267  ni_log(NI_LOG_DEBUG, "slice_arg=%d\n", p_enc->slice_arg);
12268  ni_log(NI_LOG_DEBUG, "entropy_coding_mode=%d\n", p_enc->entropy_coding_mode);
12269  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_mode=%d\n", p_enc->intra_mb_refresh_mode);
12270  ni_log(NI_LOG_DEBUG, "intra_mb_refresh_arg=%d\n", p_enc->intra_mb_refresh_arg);
12271  ni_log(NI_LOG_DEBUG, "intra_reset_refresh=%d\n", p_enc->intra_reset_refresh);
12272 
12273  ni_log(NI_LOG_DEBUG, "gop_preset_index=%d\n", p_enc->gop_preset_index);
12274 #ifndef QUADRA
12275  if (!QUADRA)
12276  {
12278  {
12279  int i;
12280  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
12281  for (i = 0; i < p_enc->custom_gop_params.custom_gop_size; i++)
12282  {
12283  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_enc->custom_gop_params.pic_param[i].pic_type);
12284  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_enc->custom_gop_params.pic_param[i].poc_offset);
12285  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_qp=%d\n", i, p_enc->custom_gop_params.pic_param[i].pic_qp);
12286  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pic_L0=%d\n", i, p_enc->custom_gop_params.pic_param[i].num_ref_pic_L0);
12287  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L0=%d\n", i, p_enc->custom_gop_params.pic_param[i].ref_poc_L0);
12288  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].ref_poc_L1=%d\n", i, p_enc->custom_gop_params.pic_param[i].ref_poc_L1);
12289  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_enc->custom_gop_params.pic_param[i].temporal_id);
12290  }
12291  }
12292  }
12293  else // QUADRA
12294 #endif
12295  {
12297  {
12298  int i, j;
12299  ni_log(NI_LOG_DEBUG, "custom_gop_params.custom_gop_size=%d\n", p_enc->custom_gop_params.custom_gop_size);
12300  for (i = 0; i < NI_MAX_GOP_NUM; i++)
12301  {
12302  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].poc_offset=%d\n", i, p_enc->custom_gop_params.pic_param[i].poc_offset);
12303  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_offset=%d\n", i, p_enc->custom_gop_params.pic_param[i].qp_offset);
12304  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].qp_factor=%lf\n", i, p_enc->custom_gop_params.pic_param[i].qp_factor);
12305  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].temporal_id=%d\n", i, p_enc->custom_gop_params.pic_param[i].temporal_id);
12306  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].pic_type=%d\n", i, p_enc->custom_gop_params.pic_param[i].pic_type);
12307  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].num_ref_pics=%d\n", i, p_enc->custom_gop_params.pic_param[i].num_ref_pics);
12308  for (j = 0; j < NI_MAX_REF_PIC; j++)
12309  {
12310  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic=%d\n", i, j, p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic);
12311  ni_log(NI_LOG_DEBUG, "custom_gop_params.pic_param[%d].rps[%d].ref_pic_used=%d\n", i, j, p_enc->custom_gop_params.pic_param[i].rps[j].ref_pic_used);
12312  }
12313  }
12314  }
12315  }
12316 
12317  return;
12318 }
12319 
12320 /*!******************************************************************************
12321  * \brief decoder keep alive thread function triggers every 1 second
12322  *
12323  * \param void thread args
12324  *
12325  * \return void
12326  *******************************************************************************/
12327 void *ni_session_keep_alive_thread(void *arguments)
12328 {
12330  ni_thread_arg_struct_t *args = (ni_thread_arg_struct_t *)arguments;
12331  ni_session_stats_t inst_info = {0};
12332  ni_session_context_t ctx = {0};
12333  uint64_t endtime = ni_gettime_ns();
12334  uint64_t current_time;
12335  ni_pthread_mutex_t *p_mutex;
12336  //interval(nanoseconds) is equals to ctx.keep_alive_timeout/3(330,000,000ns approximately equal to 1/3 second).
12337  uint64_t interval = args->keep_alive_timeout * 330000000LL;
12338 #ifndef _ANDROID
12339 #ifdef __linux__
12340  struct sched_param sched_param;
12341 
12342  // Linux has a wide variety of signals, Windows has a few.
12343  // A large number of signals will interrupt the thread, which will cause heartbeat command interval more than 1 second.
12344  // So just mask the unuseful signals in Linux
12345  sigset_t signal;
12346  sigfillset(&signal);
12347  ni_pthread_sigmask(SIG_BLOCK, &signal, NULL);
12348 
12349  /* set up schedule priority
12350  * first try to run with RR mode.
12351  * if fails, try to set nice value.
12352  * if fails either, ignore it and run with default priority.
12353  * Note: Scheduling requires root permission. App is probably exectued
12354  * without root so the priority for this thread might just end up
12355  * being default.
12356  */
12357  if (((sched_param.sched_priority = sched_get_priority_max(SCHED_RR)) ==
12358  -1) ||
12359  sched_setscheduler(syscall(SYS_gettid), SCHED_RR, &sched_param) < 0)
12360  {
12361  ni_log(NI_LOG_DEBUG, "%s cannot set scheduler: %s\n", __func__,
12362  strerror(NI_ERRNO));
12363  if (setpriority(PRIO_PROCESS, 0, -20) != 0)
12364  {
12365  ni_log(NI_LOG_DEBUG, "%s cannot set nice value: %s\n", __func__,
12366  strerror(NI_ERRNO));
12367  }
12368  }
12369 
12370 #elif defined(_WIN32)
12371  /* set up schedule priority.
12372  * try to set the current thread to time critical level which is the highest prioriy
12373  * level.
12374  */
12375  if (SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL) ==
12376  0)
12377  {
12378  ni_log(NI_LOG_DEBUG, "%s cannot set priority: %d.\n", __func__,
12379  GetLastError());
12380  }
12381 #endif
12382 #endif
12383 #ifndef _WIN32
12384  // Set thread name, name is KAT + hw_id + session_id
12385  // hw_id need at most 2 bytes, session_id at most 4 bytes.
12386  char name[16] = {0};
12387  snprintf(name, sizeof(name), "%s%.2x%.4x", "KAT", args->hw_id, args->session_id);
12388 #if __linux__
12389  prctl(PR_SET_NAME, name);
12390 #elif __APPLE__
12391  pthread_setname_np(name);
12392 #endif
12393 #endif
12394  // Initializes the session context variables that keep alive command and query status command need.
12396  ctx.last_access_time = endtime;
12397  ctx.hw_id = args->hw_id;
12398  ctx.session_id = args->session_id;
12400  ctx.device_type = args->device_type;
12401  ctx.blk_io_handle = args->device_handle;
12402  ctx.event_handle = args->thread_event_handle;
12403  ctx.p_all_zero_buf = args->p_buffer;
12405  volatile uint64_t * plast_access_time = args->plast_access_time;
12406  if((ctx.last_access_time - *plast_access_time) >=
12407  ctx.keep_alive_timeout * 1000000000LL)
12408  {
12410  "%s creation timeout. session_id=0x%X requested timeout: %" PRIu64
12411  "ns, ping time delta: %" PRIu64 "ns\n ",
12412  __func__, ctx.session_id,
12413  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
12414  ctx.last_access_time - *plast_access_time);
12415  }
12416  ni_log(NI_LOG_DEBUG, "%s ctx.keep_alive_timeout: %us.\n", __func__,
12417  ctx.keep_alive_timeout);
12418  p_mutex = args->p_mutex;
12419 
12420  for (;;)
12421  {
12422  ni_pthread_mutex_lock(p_mutex);
12423 
12424  retval =
12426  ctx.event_handle, ctx.p_all_zero_buf);
12427 
12428  retval = ni_query_session_stats(&ctx,
12429  ctx.device_type,
12430  &inst_info,
12431  retval,
12433 
12434  if (NI_RETCODE_SUCCESS == retval)
12435  {
12438  ctx.device_type,
12439  ctx.hw_id,
12440  &(ctx.session_id));
12441  }
12442 
12443  ni_pthread_mutex_unlock(p_mutex);
12444 
12445  if(retval)
12446  {
12447  uint32_t error_status = inst_info.ui32LastTransactionCompletionStatus;
12448  if(error_status == NI_RETCODE_SUCCESS)
12449  {
12450  /* QDFWSH-971: Error is sometimes captured by keep_alive_thread
12451  but LastTransactionCompletionStatus may be overwrited and cause
12452  incorrect log. In this case, check LastErrorStatus.*/
12453  ni_log(NI_LOG_ERROR, "session_no 0x%x inst_err_no may be overwrited!\n",
12454  ctx.session_id);
12457  ctx.device_type,
12458  ctx.hw_id,
12459  &(ctx.session_id));
12460  error_status = inst_info.ui32LastErrorStatus;
12461  }
12463  "Persistent failures detected, %s() line-%d: session_no 0x%x sess_err_no %u "
12464  "inst_err_no %u\n",
12465  __func__, __LINE__, ctx.session_id, inst_info.ui16ErrorCount, error_status);
12466  LRETURN;
12467  }
12469  /*If the interval between two heartbeats is greater then expected(interval) or
12470  acceptable(timeout) then the thread might have been blocked.*/
12471  if ((current_time - ctx.last_access_time) >= (2 * interval) || //*2 is for safety
12472  (current_time - ctx.last_access_time) >=
12473  args->keep_alive_timeout * 1000000000LL)
12474  {
12475  ni_log(
12476  NI_LOG_ERROR,
12477  "%s was possibly blocked. session_id=0x%X requested timeout: %" PRIu64
12478  "ns, ping time delta: %" PRIu64 "ns\n ",
12479  __func__, ctx.session_id,
12480  (uint64_t)ctx.keep_alive_timeout * 1000000000LL,
12482  }
12483  *plast_access_time = ctx.last_access_time = current_time;
12484  if (ctx.session_id == NI_INVALID_SESSION_ID)
12485  {
12487  }
12488 
12489  // 1. If received failure, set the close_thread flag to TRUE, and exit,
12490  // then main thread will check this flag and return failure directly;
12491  // 2. skip checking VPU recovery.
12492  // If keep_alive thread detect the VPU RECOVERY before main thread,
12493  // the close_thread flag may damage the vpu recovery handling process.
12494  if ((NI_RETCODE_SUCCESS != retval) &&
12495  (NI_RETCODE_NVME_SC_VPU_RECOVERY != retval))
12496  {
12497  LRETURN;
12498  }
12499  endtime += interval;
12500  while (ni_gettime_ns() < endtime)
12501  {
12502  if (args->close_thread)
12503  {
12504  LRETURN;
12505  }
12506  ni_usleep(10000); // 10ms per loop
12507  }
12508  }
12509 
12510 END:
12511 
12512  if (NI_RETCODE_SUCCESS != retval)
12513  {
12514  ni_log(NI_LOG_ERROR, "%s abnormal closed:%d\n", __func__, retval);
12515  // changing the value to be True here means the thread has been closed.
12516  args->close_thread = true;
12517  }
12518 
12520 
12521  ni_log(NI_LOG_DEBUG, "%s(): exit\n", __func__);
12522 
12523  return NULL;
12524 }
12525 
12526 /*!******************************************************************************
12527 * \brief Open a xcoder upload instance
12528 *
12529 * \param p_ctx - pointer to caller allocated uploader session context
12530 *
12531 * \return
12532 * On success
12533 * NI_RETCODE_SUCCESS
12534 *
12535 * On failure
12536 * NI_RETCODE_INVALID_PARAM
12537 * NI_RETCODE_ERROR_MEM_ALOC
12538 * NI_RETCODE_ERROR_INVALID_SESSION
12539 * NI_RETCODE_FAILURE
12540 *******************************************************************************/
12542 {
12544  void * p_buffer = NULL;
12545  uint32_t ui32LBA = 0;
12546  uint32_t modelled_load;
12547 
12548  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
12549 
12550  if (!p_ctx)
12551  {
12552  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n",
12553  __func__);
12554  retval = NI_RETCODE_INVALID_PARAM;
12555  LRETURN;
12556  }
12557 
12558  //Create the session if the create session flag is set
12559  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
12560  {
12562  p_ctx->pts_table = NULL;
12563  p_ctx->dts_queue = NULL;
12564  p_ctx->p_leftover = NULL;
12565  p_ctx->buffer_pool = NULL;
12566  p_ctx->prev_size = 0;
12567  p_ctx->sent_size = 0;
12568  p_ctx->status = 0;
12569  p_ctx->key_frame_type = 0;
12570  p_ctx->ready_to_close = 0;
12571  p_ctx->rc_error_count = 0;
12572  p_ctx->frame_num = 0;
12573  p_ctx->pkt_num = 0;
12574  p_ctx->pkt_index = 0;
12575 
12576  //malloc zero data buffer
12577  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
12579  {
12580  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
12581  NI_ERRNO, __func__);
12582  retval = NI_RETCODE_ERROR_MEM_ALOC;
12583  LRETURN;
12584  }
12585  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
12586 
12587  //malloc data buffer
12588  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
12589  {
12590  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
12591  NI_ERRNO, __func__);
12592  retval = NI_RETCODE_ERROR_MEM_ALOC;
12593  LRETURN;
12594  }
12595  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
12596 
12597  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
12598  //In case we can open sesison, the session id would become valid.
12599  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
12600  (uint16_t)NI_INVALID_SESSION_ID;
12601 
12602  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
12603  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_ENCODER, ni_htonl(p_ctx->codec_format), 1/*1 for uploadMode*/);
12604  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
12605  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
12606  if (retval != NI_RETCODE_SUCCESS)
12607  {
12608  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
12609  LRETURN;
12610  }
12611  //Open will return a session status structure with a valid session id if it worked.
12612  //Otherwise the invalid session id set before the open command will stay
12613  p_ctx->session_id = ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
12614  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
12615  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
12616  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
12617  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
12618  {
12619  ni_log2(p_ctx, NI_LOG_ERROR,
12620  "ERROR %s(): p_ctx->device_handle=%" PRIx64
12621  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
12622  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
12623  p_ctx->session_id);
12624  ni_encoder_session_close(p_ctx, 0);
12626  LRETURN;
12627  }
12628  ni_log2(p_ctx, NI_LOG_DEBUG,
12629  "Uploader open session ID:0x%x,timestamp:%" PRIu64 "\n",
12630  p_ctx->session_id, p_ctx->session_timestamp);
12631 
12632  //Send keep alive timeout Info
12633  uint64_t keep_alive_timeout =
12634  p_ctx->keep_alive_timeout * 1000000; //send us to FW
12635  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
12636  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
12637  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
12638  keep_alive_timeout);
12640  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
12641  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
12642  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
12643  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
12644  CHECK_VPU_RECOVERY(retval);
12645 
12646  if (NI_RETCODE_SUCCESS != retval)
12647  {
12648  ni_log2(p_ctx, NI_LOG_ERROR,
12649  "ERROR %s(): nvme write keep_alive_timeout command "
12650  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
12651  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
12653  LRETURN;
12654  }
12655 
12656  // only update firmware with pixel rate if firmware >= 6rf
12657  if (ni_cmp_fw_api_ver(
12658  (char *) &p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rf") >= 0)
12659  {
12660  /* Add the modelled load of the hwuploader */
12661  if (p_ctx->framerate.framerate_denom == 0)
12662  {
12663  // unknown or variable frame rate. Assume 25 fps
12664  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height) * 25;
12665  }
12666  else
12667  {
12668  modelled_load = (p_ctx->active_video_width * p_ctx->active_video_height *
12670  }
12671 
12672  // modelled_load can overflow a 4-byte value so we will downscale it to
12673  // kilopixels per sec by shifting the modelled load down by 10 bits.
12674  modelled_load >>= 10;
12675 
12677 
12678  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
12679  memcpy(p_buffer, &modelled_load, 4);
12680 
12681  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
12682  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
12683 
12684  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
12685  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
12686  CHECK_VPU_RECOVERY(retval);
12687 
12688  if (NI_RETCODE_SUCCESS != retval)
12689  {
12691  LRETURN;
12692  }
12693  }
12694 
12695  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
12696  }
12697 
12698  // init for frame pts calculation
12699  p_ctx->is_first_frame = 1;
12700  p_ctx->last_pts = 0;
12701  p_ctx->last_dts = 0;
12702 
12703  ni_timestamp_init(p_ctx, &p_ctx->pts_table, "dec_pts");
12704  ni_timestamp_init(p_ctx, &p_ctx->dts_queue, "dec_dts");
12705 
12706  //p_ctx->active_video_width = 0;
12707  //p_ctx->active_video_height = 0;
12708 
12709  ni_log2(p_ctx, NI_LOG_DEBUG,
12710  "%s(): p_ctx->device_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
12711  "p_ctx->session_id=%d\n",
12712  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
12713  p_ctx->session_id);
12714 
12715  p_ctx->hw_action = NI_CODEC_HW_NONE;
12716 #ifndef _WIN32
12717  // If this is a P2P upload session, open the Netint kernel driver
12718  if (p_ctx->isP2P)
12719  {
12720  retval = p2p_fill_pcie_address(p_ctx);
12721  if(retval != NI_RETCODE_SUCCESS)
12722  {
12723  LRETURN;
12724  }
12725  }
12726 #endif
12727 
12728 END:
12729 
12730  ni_aligned_free(p_buffer);
12731  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
12732  return retval;
12733 }
12734 
12735 /*!******************************************************************************
12736 * \brief Copy a xcoder decoder worker thread info
12737 *
12738 * \param
12739 *
12740 * \return
12741 *******************************************************************************/
12743 {
12744  if (!src_p_ctx || !dst_p_ctx)
12745  {
12746  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null!, return\n", __func__);
12747  return NI_RETCODE_INVALID_PARAM;
12748  }
12749 
12750  dst_p_ctx->pext_mutex = src_p_ctx->pext_mutex; //for hwdl
12751  dst_p_ctx->max_nvme_io_size = src_p_ctx->max_nvme_io_size;
12752  dst_p_ctx->device_handle = src_p_ctx->device_handle;
12753  dst_p_ctx->blk_io_handle = src_p_ctx->blk_io_handle;
12754  dst_p_ctx->hw_id = src_p_ctx->hw_id;
12755  dst_p_ctx->session_timestamp = src_p_ctx->session_timestamp;
12756  memcpy(dst_p_ctx->fw_rev, src_p_ctx->fw_rev, sizeof(src_p_ctx->fw_rev));
12757  if (src_p_ctx->isP2P)
12758  {
12759  dst_p_ctx->isP2P = src_p_ctx->isP2P;
12760  dst_p_ctx->ddr_config = src_p_ctx->ddr_config;
12761  dst_p_ctx->domain = src_p_ctx->domain;
12762  dst_p_ctx->bus = src_p_ctx->bus;
12763  dst_p_ctx->dev = src_p_ctx->dev;
12764  dst_p_ctx->fn = src_p_ctx->fn;
12765  dst_p_ctx->netint_fd = src_p_ctx->netint_fd;
12766  }
12767 
12768  return NI_RETCODE_SUCCESS;
12769 }
12770 
12771 /*!******************************************************************************
12772 * \brief Send a YUV p_frame to upload session
12773 *
12774 * \param
12775 *
12776 * \return
12777 *******************************************************************************/
12779  niFrameSurface1_t *hwdesc)
12780 {
12781  int retval = 0;
12782  uint32_t size = 0;
12783  //uint32_t metadata_size = NI_APP_ENC_FRAME_META_DATA_SIZE;
12784  uint32_t i = 0;
12785  uint32_t sent_size = 0;
12786  uint32_t frame_size_bytes = 0;
12787  uint32_t retry_count = 0;
12788  ni_instance_buf_info_t buf_info = { 0 };
12789 
12790  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
12791  //niFrameSurface1_t* p_data3 = (niFrameSurface1_t*)((uint8_t*)p_frame->p_data[3]);
12793  //ni_log2(p_ctx, NI_LOG_DEBUG, "%s:mar16 HW=%d ui16FrameIdx=%d i8InstID=%d device_handle=%d\n",
12794  // ishwframe, p_data3->ui16FrameIdx, p_data3->i8InstID, p_data3->device_handle);
12795 
12796  if (!p_ctx || !p_frame)
12797  {
12798  ni_log(NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
12799  __func__);
12800  retval = NI_RETCODE_INVALID_PARAM;
12801  LRETURN;
12802  }
12803 
12804  uint8_t separate_metadata = p_frame->separate_metadata;
12805  uint8_t separate_start = (p_frame->separate_start && p_frame->total_start_len) ? 1 : 0;
12806 
12807  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
12808  {
12809  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
12810  __func__);
12812  LRETURN;
12813  }
12814 
12815 #ifdef MEASURE_LATENCY
12816  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
12817  {
12818  uint64_t abs_time_ns = ni_gettime_ns();
12820  abs_time_ns, p_frame->pts);
12821  }
12822 #endif
12823 
12824  frame_size_bytes = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2];// +p_frame->data_len[3] + p_frame->extra_data_len;
12825  ni_log2(p_ctx, NI_LOG_DEBUG, "frame size bytes =%u %d is metadata!\n", frame_size_bytes,
12826  0);
12827  p_ctx->status = 0;
12828 
12829  if (p_frame->end_of_stream)
12830  {
12831  retval = NI_RETCODE_SUCCESS;
12832  LRETURN;
12833  }
12834 
12835  for (;;)
12836  {
12837  query_sleep(p_ctx);
12838 
12840  NI_DEVICE_TYPE_ENCODER, &buf_info);
12841  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
12842  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
12843  if (NI_RETCODE_SUCCESS != retval ||
12844  (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count >= 500))
12845  {
12846  if (retry_count >= 500)
12847  {
12848  ni_log2(p_ctx, NI_LOG_DEBUG,
12849  "hwupload write exceeded max query retries. rc=%d try=%d"
12850  "\n",
12851  retval, retry_count);
12852  }
12853  retval = NI_RETCODE_ERROR_MEM_ALOC;
12855  LRETURN;
12856  }
12857  if (buf_info.hw_inst_ind.buffer_avail == 0 && retry_count < 500)
12858  {
12859  retry_count++;
12860  ni_pthread_mutex_unlock(&p_ctx->mutex);
12861  ni_usleep(100);
12862  ni_pthread_mutex_lock(&p_ctx->mutex);
12863  } else //available
12864  {
12865  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: avail %d, FID %d\n", __func__,
12866  buf_info.hw_inst_ind.buffer_avail,
12867  buf_info.hw_inst_ind.frame_index);
12868  break;
12869  }
12870  }
12871  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload write query success, available buf "
12872  "size %u >= frame size %u , retry %u\n",
12873  buf_info.buf_avail_size, frame_size_bytes, retry_count);
12874 
12875  {
12876 #ifdef XCODER_TIMESTAMP_DTS_ENABLED
12877  retval = ni_timestamp_register(p_ctx->buffer_pool, p_ctx->dts_queue,
12878  p_frame->dts, 0);
12879  if (NI_RETCODE_SUCCESS != retval)
12880  {
12881  ni_log2(p_ctx, NI_LOG_ERROR,
12882  "ERROR %s(): ni_timestamp_register() for dts "
12883  "returned: %d\n",
12884  __func__, retval);
12885  }
12886 #endif
12887 
12888  //Apply write configuration here
12889  retval = ni_config_session_rw(p_ctx, SESSION_WRITE_CONFIG, 1,
12890  NI_CODEC_HW_UPLOAD, 0);
12891  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
12892  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
12893  CHECK_VPU_RECOVERY(retval);
12894 
12895  if (separate_metadata)
12896  {
12898  "6S") < 0)
12899  {
12900  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): uploader separated metadata not supported on device with FW api version < 6.S\n",
12901  __func__);
12903  LRETURN;
12904  }
12905 
12906  if (!p_frame->p_metadata_buffer)
12907  {
12908  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_metadata_buffer is NULL, allocation failed?\n",
12909  __func__);
12910  retval = NI_RETCODE_ERROR_MEM_ALOC;
12911  LRETURN;
12912  }
12913 
12914  // fill in metadata
12915  ni_metadata_enc_frame_t *p_meta;
12916  p_meta = (ni_metadata_enc_frame_t *)p_frame->p_metadata_buffer;
12917 
12918  if (separate_start)
12919  {
12920  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
12921  p_meta->start_len[i] = p_frame->start_len[i];
12922  }
12923  else
12924  {
12925  memset(p_meta->start_len, 0, sizeof(p_meta->start_len));
12926  }
12928 
12929  ni_log(
12930  NI_LOG_DEBUG,
12931  "%s(): %d.%u p_ctx->frame_num=%" PRIu64 ", "
12932  "p_frame->video_width=%u, p_frame->video_height=%u, "
12933  "start_len [%u,%u,%u] inconsecutive_transfer %u\n",
12934  __func__, p_ctx->hw_id, p_ctx->session_id, p_ctx->frame_num,
12935  p_frame->video_width, p_frame->video_height,
12936  p_meta->start_len[0], p_meta->start_len[1], p_meta->start_len[2],
12937  p_meta->inconsecutive_transfer);
12938 
12939  uint32_t ui32LBA_metadata =
12941  ni_log2(p_ctx, NI_LOG_DEBUG,
12942  "%s: p_metadata_buffer = %p, metadata_buffer_size "
12943  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
12944  __func__, p_frame->p_metadata_buffer,
12945  p_frame->metadata_buffer_size, p_ctx->frame_num,
12946  ui32LBA_metadata);
12947 
12948  sent_size =
12950 
12951  retval = ni_nvme_send_write_cmd(
12952  p_ctx->blk_io_handle, p_ctx->event_handle,
12953  p_frame->p_metadata_buffer, sent_size,
12954  ui32LBA_metadata);
12955  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
12956  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
12957  CHECK_VPU_RECOVERY(retval);
12958  if (retval < 0)
12959  {
12960  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n",
12961  __func__);
12963  LRETURN;
12964  }
12965  }
12966 
12967  if (separate_start)
12968  {
12969  if (!p_frame->p_start_buffer)
12970  {
12971  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_start_buffer is NULL, allocation failed?\n",
12972  __func__);
12973  retval = NI_RETCODE_ERROR_MEM_ALOC;
12974  LRETURN;
12975  }
12976 
12977  uint32_t ui32LBA =
12979  ni_log2(p_ctx, NI_LOG_DEBUG,
12980  "%s: p_start_buffer = %p, p_frame->start_buffer_size "
12981  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
12982  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size, p_ctx->frame_num,
12983  ui32LBA);
12984 
12985  sent_size =
12987 
12988  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
12989  p_frame->p_start_buffer, sent_size, ui32LBA);
12990  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
12991  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
12992  CHECK_VPU_RECOVERY(retval);
12993  if (retval < 0)
12994  {
12995  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
12997  LRETURN;
12998  }
12999  }
13000 
13001  if (p_frame->inconsecutive_transfer)
13002  {
13003  uint32_t ui32LBA =
13005 
13006  for (i = 0; i < NI_MAX_NUM_SW_FRAME_DATA_POINTERS; i++)
13007  {
13008  ni_log2(p_ctx, NI_LOG_DEBUG,
13009  "%s: p_data = %p, p_frame->buffer_size "
13010  "= %u, p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13011  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
13012  ui32LBA);
13013 
13014  if (p_frame->data_len[i])
13015  {
13016  sent_size = p_frame->data_len[i];
13017  if (separate_start)
13018  sent_size -= p_frame->start_len[i];
13019 
13020  sent_size =
13022 
13023  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13024  p_frame->p_data[i]+p_frame->start_len[i], sent_size, ui32LBA);
13025  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13026  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13027  CHECK_VPU_RECOVERY(retval);
13028  if (retval < 0)
13029  {
13030  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
13032  LRETURN;
13033  }
13034  }
13035  }
13036  }
13037  else
13038  {
13039  uint32_t ui32LBA =
13041  ni_log2(p_ctx, NI_LOG_DEBUG,
13042  "%s: p_data = %p, p_frame->buffer_size = %u, "
13043  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
13044  __func__, p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
13045  ui32LBA);
13046 
13047  sent_size = frame_size_bytes;
13048  if (separate_start)
13049  sent_size -= p_frame->total_start_len;
13050 
13051  sent_size =
13053 
13054  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13055  p_frame->p_buffer+p_frame->total_start_len, sent_size, ui32LBA);
13056  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
13057  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13058  CHECK_VPU_RECOVERY(retval);
13059  if (retval < 0)
13060  {
13061  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
13063  LRETURN;
13064  }
13065  }
13066 
13067  hwdesc->ui16FrameIdx = buf_info.hw_inst_ind.frame_index;
13068  hwdesc->ui16session_ID = p_ctx->session_id;
13069  hwdesc->device_handle =
13070  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13071  hwdesc->bit_depth = p_ctx->bit_depth_factor;
13072  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
13073  hwdesc->output_idx = hwdesc->ui32nodeAddress = 0;
13074 
13075  p_ctx->frame_num++;
13076  size = frame_size_bytes;
13077 
13078 #ifdef XCODER_DUMP_DATA
13079  char dump_file[256];
13080  snprintf(dump_file, sizeof(dump_file), "%ld-%u-hwup-fme/fme-%04ld.yuv",
13081  (long)getpid(), p_ctx->session_id, (long)p_ctx->frame_num);
13082 
13083  FILE *f = fopen(dump_file, "wb");
13084  fwrite(p_frame->p_buffer,
13085  p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2],
13086  1, f);
13087  fflush(f);
13088  fclose(f);
13089 #endif
13090  }
13091 
13092 #ifdef MEASURE_LATENCY
13093  if ((p_frame->pts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
13094  {
13095  uint64_t abs_time_ns = ni_gettime_ns();
13097  ni_log(NI_LOG_INFO, "PTS:%" PRId64 ",DELTA:%" PRId64 ",uLAT:%" PRIu64 ";\n",
13098  p_frame->pts, abs_time_ns - q->last_benchmark_time,
13099  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->pts));
13100  q->last_benchmark_time = abs_time_ns;
13101  }
13102 #endif
13103 
13104  retval = size;
13105 
13106 END:
13107 
13108  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
13109  return retval;
13110 }
13111 
13112 /*!******************************************************************************
13113 * \brief Retrieve a HW descriptor of uploaded frame
13114 *
13115 * \param p_ctx pointer to uploader session context
13116 * hwdesc pointer to hw descriptor
13117 *
13118 * \return
13119 * On success
13120 * NI_RETCODE_SUCCESS
13121 * On failure
13122 * NI_RETCODE_INVALID_PARAM
13123 * NI_RETCODE_ERROR_INVALID_SESSION
13124 * NI_RETCODE_FAILURE
13125 *******************************************************************************/
13127  niFrameSurface1_t *hwdesc)
13128 {
13129  int retval = 0;
13130  ni_instance_buf_info_t hwdesc_info = { 0 };
13131  int query_retry = 0;
13132 
13133  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13134 
13135  if (!p_ctx || !hwdesc)
13136  {
13137  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
13138  __func__);
13139  retval = NI_RETCODE_INVALID_PARAM;
13140  LRETURN;
13141  }
13142 
13143  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13144  {
13145  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13146  __func__);
13148  LRETURN;
13149  }
13150 
13151  for (;;)
13152  {
13153  query_sleep(p_ctx);
13154 
13155  query_retry++;
13156 #ifndef _WIN32
13158  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
13159 #else
13161  NI_DEVICE_TYPE_ENCODER, &hwdesc_info);
13162 #endif
13163  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13164  p_ctx->device_type, p_ctx->hw_id,
13165  &(p_ctx->session_id), OPT_1);
13166 
13167  if (NI_RETCODE_SUCCESS != retval)
13168  {
13169  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning upload read hwdesc fail rc %d or ind "
13170  "!\n", retval);
13171 
13172  if (query_retry >= 1000)
13173  {
13174  retval = NI_RETCODE_FAILURE;
13175  LRETURN;
13176  }
13177  ni_usleep(100);
13178  }
13179  else
13180  {
13181  ni_log2(p_ctx, NI_LOG_DEBUG, "Info hwupload read hwdesc success, "
13182  "frame_ind=%d !\n", hwdesc_info.hw_inst_ind.frame_index);
13183 
13184  hwdesc->ui16FrameIdx = hwdesc_info.hw_inst_ind.frame_index;
13185  hwdesc->ui16session_ID = p_ctx->session_id;
13186  hwdesc->device_handle =
13187  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13188  hwdesc->bit_depth = p_ctx->bit_depth_factor;
13189  hwdesc->src_cpu = (uint8_t)NI_DEVICE_TYPE_ENCODER;
13190  hwdesc->output_idx = 0;
13191  LRETURN;
13192  }
13193  }
13194 
13195 END:
13196  return retval;
13197 }
13198 
13199 /*!*****************************************************************************
13200 * \brief clear a particular xcoder instance buffer/data
13201 *
13202 * \param ni_session_context_t p_ctx - xcoder Context
13203 * \param ni_instance_buf_info_rw_type_t rw_type
13204 * \param ni_device_type_t device_type - xcoder type Encoder or Decoder
13205 * \param ni_instance_buf_info_t *out - Struct preallocated from the caller
13206 * where the resulting data will be placed
13207 *
13208 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION,
13209 * NI_RETCODE_ERROR_MEM_ALOC or NI_RETCODE_ERROR_NVME_CMD_FAILED on
13210 * failure
13211 ******************************************************************************/
13213 {
13214  void* p_buffer = NULL;
13216  uint32_t ui32LBA = 0;
13217 
13218  ni_log(NI_LOG_TRACE, "%s(): enter - device_handle %d\n", __func__,
13219  surface->device_handle);
13220 
13221  if ((uint16_t)NI_INVALID_SESSION_ID == surface->ui16session_ID)
13222  {
13223  ni_log(NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13224  __func__);
13226  LRETURN;
13227  }
13228 
13229  //malloc data buffer
13230  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
13231  {
13232  ni_log(NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
13233  NI_ERRNO, __func__);
13234  retval = NI_RETCODE_ERROR_MEM_ALOC;
13235  LRETURN;
13236  }
13237  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
13238  ni_log(NI_LOG_DEBUG, "%s(): FID = %d\n", __func__, surface->ui16FrameIdx);
13239  ui32LBA = CLEAR_INSTANCE_BUF_W(((uint16_t)surface->ui16FrameIdx));
13240  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)surface->device_handle,
13241  NI_INVALID_DEVICE_HANDLE, p_buffer,
13242  NI_DATA_BUFFER_LEN, ui32LBA);
13243  //Cannot check sessio stats here since this isn't a session command.
13244  if (retval < 0)
13245  {
13246  ni_log(NI_LOG_ERROR, "[session_id=0x%x,time_stamp=%" PRIu64 "] " "%s(): NVME command Failed\n", surface->ui16session_ID, ni_log_get_utime(), __func__);
13248  LRETURN;
13249  }
13250 
13251 END:
13252 
13253  ni_aligned_free(p_buffer);
13254  ni_log(NI_LOG_TRACE, "%s(): exit\n", __func__);
13255  return retval;
13256 }
13257 
13258 /*!******************************************************************************
13259 * \brief Retrieve a hw desc p_frame from decoder
13260 * \param
13261 *
13262 * \return
13263 *******************************************************************************/
13265 {
13266  //Needs serious editing to support hwdesc read again, this is currently vanilla read
13267  //queue_info decoder_read_workerqueue;
13268  ni_instance_mgr_stream_info_t data = {0};
13269  int rx_size = 0;
13270  uint64_t frame_offset = 0;
13271  uint8_t *p_data_buffer = NULL;
13272  int i = 0;
13273  int retval = NI_RETCODE_SUCCESS;
13274  int metadata_hdr_size = NI_FW_META_DATA_SZ -
13276  int sei_size = 0;
13277  uint32_t total_bytes_to_read = 0;
13278  uint32_t total_yuv_met_size = 0;
13279  uint32_t read_size_bytes = 0;
13280  ni_instance_buf_info_t buf_info = {0};
13281  int query_retry = 0;
13282  uint32_t ui32LBA = 0;
13283  unsigned int bytes_read_so_far = 0;
13284  int query_type = INST_BUF_INFO_RW_READ;
13285  int low_delay_notify = 0;
13286  ni_session_statistic_t sessionStatistic = {0};
13287  uint32_t frames_dropped = 0;
13288  ni_xcoder_params_t *p_param;
13289  uint8_t get_first_metadata = 0;
13290  uint8_t sequence_change = 0;
13291 
13292  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
13293 
13294  if (!p_ctx || !p_frame)
13295  {
13296  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
13297  __func__);
13298  return NI_RETCODE_INVALID_PARAM;
13299  }
13300 
13301  ni_pthread_mutex_lock(&p_ctx->mutex);
13302 
13303 start:
13304  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
13305  {
13306  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
13307  __func__);
13309  LRETURN;
13310  }
13311 
13312  p_param = (ni_xcoder_params_t *)p_ctx->p_session_config;
13313  p_data_buffer = (uint8_t *)p_frame->p_buffer;
13314 
13315  // p_frame->p_data[] can be NULL before actual resolution is returned by
13316  // decoder and buffer pool is allocated, so no checking here.
13317 
13318  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
13319  total_yuv_met_size = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + metadata_hdr_size;
13320  ni_log2(p_ctx, NI_LOG_DEBUG,
13321  "Total bytes to read %u total_yuv_met_size %u, low_delay %u\n",
13322  total_bytes_to_read, total_yuv_met_size, p_ctx->decoder_low_delay);
13323  if (p_ctx->decoder_low_delay > 0 && !p_ctx->ready_to_close)
13324  {
13325  ni_log2(p_ctx, NI_LOG_DEBUG, "frame_num = %" PRIu64 ", pkt_num = %" PRIu64 "\n",
13326  p_ctx->frame_num, p_ctx->pkt_num);
13327  frames_dropped = p_ctx->session_statistic.ui32FramesDropped;
13328  if (p_ctx->force_low_delay && (p_ctx->force_low_delay_cnt < frames_dropped)) {
13329  p_ctx->force_low_delay_cnt = frames_dropped;
13330  }
13331  if (p_ctx->frame_num + p_ctx->force_low_delay_cnt
13332  >= p_ctx->pkt_num)
13333  {
13334  //nothing to query, leave
13335  retval = NI_RETCODE_SUCCESS;
13336  LRETURN;
13337  }
13338  query_type = INST_BUF_INFO_RW_READ_BUSY;
13339  }
13340  for (;;)
13341  {
13342  query_sleep(p_ctx);
13343 
13344  query_retry++;
13345 
13346  if (ni_cmp_fw_api_ver(
13348  "6r3") >= 0)
13349  {
13351  &sessionStatistic);
13352  CHECK_ERR_RC(p_ctx, retval, &sessionStatistic,
13354  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
13355  CHECK_VPU_RECOVERY(retval);
13356 
13357  buf_info.buf_avail_size = sessionStatistic.ui32RdBufAvailSize;
13358  } else
13359  {
13360  retval = ni_query_instance_buf_info(p_ctx, query_type,
13361  NI_DEVICE_TYPE_DECODER, &buf_info);
13362  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13363  p_ctx->device_type, p_ctx->hw_id,
13364  &(p_ctx->session_id), OPT_1);
13365  CHECK_VPU_RECOVERY(retval);
13366  }
13367 
13368  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc query buf_info.size = %u\n",
13369  buf_info.buf_avail_size);
13370 
13371  if (NI_RETCODE_SUCCESS != retval)
13372  {
13373  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc failed. Retry %d\n", query_retry);
13374 
13375  if (query_retry >= 1000)
13376  {
13377  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
13378  "\n", query_retry, retval);
13379  p_ctx->max_retry_fail_count[1]++;
13380  low_delay_notify = 1;
13382  LRETURN;
13383  }
13384  ni_pthread_mutex_unlock(&p_ctx->mutex);
13386  ni_pthread_mutex_lock(&p_ctx->mutex);
13387  } else if (buf_info.buf_avail_size == DP_IPC_PASSTHRU)
13388  {
13389  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Bad available buffer size %u\n", __FUNCTION__, buf_info.buf_avail_size);
13390  retval = NI_RETCODE_FAILURE;
13391  LRETURN;
13392  } else if (buf_info.buf_avail_size == metadata_hdr_size)
13393  {
13394  ni_log2(p_ctx, NI_LOG_DEBUG,
13395  "Dec read desc only hdr metadata is available. Seq change may "
13396  "have occured.\n");
13397  total_bytes_to_read = metadata_hdr_size;
13398  sequence_change = 1;
13399  break;
13400  } else if (buf_info.buf_avail_size < total_yuv_met_size)
13401  {
13402  ni_log2(p_ctx, NI_LOG_TRACE, "Dec read desc buf_size < frame_size. Retry %d\n", query_retry);
13403 
13404  // query to see if it is eos now, if we have sent it
13405  if (p_ctx->ready_to_close)
13406  {
13407  ni_log2(p_ctx, NI_LOG_TRACE,
13408  "Dec read desc query, ready_to_close %u, query eos\n",
13409  p_ctx->ready_to_close);
13410  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
13411  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13412  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13413  CHECK_VPU_RECOVERY(retval);
13414 
13415  if (data.is_flushed ||
13416  query_retry >=
13418  {
13419  if (query_retry >=
13421  {
13422  ni_log2(p_ctx, NI_LOG_ERROR,
13423  "WARNING: Dec read desc query eos reached but exceeded max "
13424  "retries. is_flushed=%u try=%d.\n",
13425  data.is_flushed, query_retry);
13426  } else
13427  {
13428  ni_log2(p_ctx, NI_LOG_DEBUG,
13429  "Dec read desc query eos reached. is_flushed=%u try=%d"
13430  "\n",
13431  data.is_flushed, query_retry);
13432  }
13433  p_frame->end_of_stream = 1;
13434  low_delay_notify = 1;
13435  retval = NI_RETCODE_SUCCESS;
13436  LRETURN;
13437  }
13438  else
13439  {
13440  ni_log2(p_ctx, NI_LOG_TRACE,
13441  "Dec read desc available buf size == %d, query try %d, "
13442  "retrying...\n",
13443  buf_info.buf_avail_size, query_retry);
13444  ni_pthread_mutex_unlock(&p_ctx->mutex);
13446  ni_pthread_mutex_lock(&p_ctx->mutex);
13447  continue;
13448  }
13449  }
13450 
13452  (p_ctx->decoder_low_delay > 0 &&
13453  ((p_ctx->frame_num + p_ctx->force_low_delay_cnt)
13454  < p_ctx->pkt_num))) &&
13455  query_retry < 1000 / 2)
13456  {
13457  if (p_ctx->decoder_low_delay && p_ctx->force_low_delay) {
13458  if (p_ctx->session_statistic.ui32FramesDropped > frames_dropped) {
13459  // last pkt sent to decoder marked as dropped, no output,
13460  // so just stop query and return
13461  p_ctx->force_low_delay_cnt++;
13462  low_delay_signal(p_ctx);
13463  retval = NI_RETCODE_SUCCESS;
13464  LRETURN;
13465  }
13466  }
13467  ni_pthread_mutex_unlock(&p_ctx->mutex);
13469  ni_pthread_mutex_lock(&p_ctx->mutex);
13470 
13471  continue;
13472  } else
13473  {
13474  if (p_ctx->decoder_low_delay > 0)
13475  {
13476  if (p_ctx->force_low_delay) {
13477  p_ctx->force_low_delay_cnt++;
13478  low_delay_signal(p_ctx);
13479  } else {
13480  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: low delay mode with non sequential "
13481  "input (B frames)? Just cancel the low delay mode then\n");
13482  // Here it should be the last signal to release the send thread
13483  // holding the low delay mutex.
13484  low_delay_signal(p_ctx);
13485  p_ctx->decoder_low_delay = 0;
13486  }
13487  }
13488 
13489  if ((p_param->dec_input_params.min_packets_delay && p_ctx->pkt_delay_cnt))
13490  {
13491  if(p_ctx->pkt_num >= (p_ctx->frame_num + p_ctx->pkt_delay_cnt +
13493  {
13494  if(query_retry <= 2000)
13495  {
13496  ni_pthread_mutex_unlock(&p_ctx->mutex);
13497  ni_usleep(25);
13498  ni_pthread_mutex_lock(&p_ctx->mutex);
13499  continue;
13500  } else {
13501  p_ctx->pkt_delay_cnt++;
13502  ni_log2(p_ctx, NI_LOG_ERROR,
13503  "Warning: decoder pkt_num %u frame_num %u "
13504  "timeout, increaing pkt_delay_cnt to %u\n",
13505  p_ctx->pkt_num, p_ctx->frame_num,
13506  p_ctx->pkt_delay_cnt);
13507  }
13508  }
13509  }
13510  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning: dec read desc failed %d retries. rc=%d"
13511  "\n", query_retry, retval);
13512  }
13513  retval = NI_RETCODE_SUCCESS;
13514  LRETURN;
13515  }
13516  else
13517  {
13518  // We have to ensure there are adequate number of DTS for picture
13519  // reorder delay otherwise wait for more packets to be sent to decoder.
13520  ni_timestamp_table_t *p_dts_queue = p_ctx->dts_queue;
13521  if ((int)p_dts_queue->list.count < p_ctx->pic_reorder_delay + 1 &&
13522  !p_ctx->ready_to_close &&
13524  {
13525  retval = NI_RETCODE_SUCCESS;
13526  ni_log2(p_ctx, NI_LOG_DEBUG,
13527  "At least %d packets should be sent before reading the "
13528  "first frame!\n",
13529  p_ctx->pic_reorder_delay + 1);
13530  LRETURN;
13531  }
13532  p_ctx->max_retry_fail_count[1] = 0;
13533 
13534  // get actual YUV transfer size if this is the stream's very first read
13535  if (0 == p_ctx->active_video_width || 0 == p_ctx->active_video_height)
13536  {
13537  retval = ni_query_stream_info(p_ctx, NI_DEVICE_TYPE_DECODER, &data);
13538  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
13539  p_ctx->device_type, p_ctx->hw_id,
13540  &(p_ctx->session_id), OPT_1);
13541  CHECK_VPU_RECOVERY(retval);
13542 
13543  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV query, pic size %ux%u xfer frame size "
13544  "%ux%u frame-rate %u is_flushed %u\n",
13545  data.picture_width, data.picture_height,
13547  data.frame_rate, data.is_flushed);
13550  p_ctx->actual_video_width = data.picture_width;
13551  p_ctx->pixel_format = data.pix_format;
13553  //p_ctx->bit_depth_factor = data.transfer_frame_stride / data.picture_width;
13554  p_ctx->is_first_frame = 1;
13555  p_ctx->pixel_format_changed = 0;
13556 
13557  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec YUV, adjust frame size from %ux%u to "
13558  "%ux%u\n", p_frame->video_width, p_frame->video_height,
13559  p_ctx->active_video_width, p_ctx->active_video_height);
13560 
13561  retval = ni_frame_buffer_alloc(
13562  p_frame, p_ctx->actual_video_width, p_ctx->active_video_height,
13563  p_ctx->codec_format == NI_CODEC_FORMAT_H264, 1,
13564  p_ctx->bit_depth_factor,
13565  3, // Alloc space for write to data[3] and metadata
13566  1);
13567 
13568  if (NI_RETCODE_SUCCESS != retval)
13569  {
13570  LRETURN;
13571  }
13572  total_bytes_to_read = p_frame->data_len[3] + metadata_hdr_size;
13573  p_data_buffer = (uint8_t*)p_frame->p_buffer;
13574  // make sure we don't read more than available
13575  ni_log2(p_ctx, NI_LOG_DEBUG, "Info dec buf size: %u YUV frame + meta-hdr size: %u "
13576  "available: %u\n", p_frame->buffer_size,
13577  total_bytes_to_read, buf_info.buf_avail_size);
13578  }
13579  break;
13580  }
13581  }// end while1 query retry
13582 
13583  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
13584  "%u cb len %u hdr %d\n",
13585  total_bytes_to_read, p_ctx->max_nvme_io_size,
13586  p_frame->data_len[0], p_frame->data_len[1],
13587  p_frame->data_len[2], metadata_hdr_size);
13588 
13589  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->data_len[3] = %u\n", p_frame->data_len[3]);
13590  if (buf_info.buf_avail_size < total_bytes_to_read)
13591  {
13592  ni_pthread_mutex_unlock(&p_ctx->mutex);
13593 
13594  ni_log2(p_ctx, NI_LOG_ERROR,
13595  "ERROR %s() avaliable size(%u) less than "
13596  "needed (%u)\n",
13597  __func__, buf_info.buf_avail_size, total_bytes_to_read);
13598  abort();
13599  } else if (total_bytes_to_read == metadata_hdr_size && !p_ctx->frame_num)
13600  {
13601  if (ni_cmp_fw_api_ver(
13603  "6rE") >= 0)
13604  {
13605  // allocate p_data_buffer to read the first metadata
13606  void *p_metadata_buffer = NULL;
13607  int buffer_size = ((metadata_hdr_size + (NI_MEM_PAGE_ALIGNMENT - 1)) /
13609  if (ni_posix_memalign(&p_metadata_buffer, sysconf(_SC_PAGESIZE), buffer_size))
13610  {
13611  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate metadata buffer.\n",
13612  NI_ERRNO, __func__);
13613  retval = NI_RETCODE_ERROR_MEM_ALOC;
13614  LRETURN;
13615  }
13616  p_data_buffer = (uint8_t *)p_metadata_buffer;
13617  get_first_metadata = 1;
13618  sequence_change = 0;
13619  }
13620  }
13621 
13622  //Apply read configuration here
13623  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 1,
13624  NI_CODEC_HW_ENABLE, 0);
13625  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, p_ctx->device_type,
13626  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13627  CHECK_VPU_RECOVERY(retval);
13628 
13629  read_size_bytes = total_bytes_to_read;
13631  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
13632  {
13633  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
13634  }
13635 
13636  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
13637  p_data_buffer, read_size_bytes, ui32LBA);
13638  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
13639  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
13640  CHECK_VPU_RECOVERY(retval);
13641  if (retval < 0)
13642  {
13643  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
13645  LRETURN;
13646  } else if (get_first_metadata) {
13647  // got first metadata alone
13648  ni_metadata_dec_frame_t *p_meta =
13649  (ni_metadata_dec_frame_t *)((uint8_t *)p_data_buffer);
13650  ni_log2(p_ctx, NI_LOG_DEBUG, "Got first pkt_delay_cnt %u\n",
13651  p_meta->metadata_common.pkt_delay_cnt);
13652  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
13653  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
13654  get_first_metadata = 0;
13655  ni_aligned_free(p_data_buffer);
13656  goto start;
13657  } else
13658  {
13659  // command issued successfully, now exit
13660  ni_metadata_dec_frame_t *p_meta;
13661  p_meta =
13662  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
13663  p_frame->data_len[0] +
13664  p_frame->data_len[1] +
13665  p_frame->data_len[2] +
13666  p_frame->data_len[3]);
13667 
13668  if (buf_info.buf_avail_size != metadata_hdr_size)
13669  {
13670  low_delay_notify = 1;
13671  // shift metadata to end of triple output
13672 #ifdef _WIN32
13673  p_data_buffer = (uint8_t *)p_frame->p_buffer +
13675  memcpy(p_meta, p_data_buffer, metadata_hdr_size);
13676 #else
13677  memcpy(p_meta,
13678  p_frame->p_buffer +
13680  metadata_hdr_size);
13681 #endif
13682  sei_size = p_meta->sei_size;
13683  niFrameSurface1_t *p_data3 =
13684  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
13685  p_frame->data_len[0] +
13686  p_frame->data_len[1] +
13687  p_frame->data_len[2]);
13688 
13689  niFrameSurface1_t *p_data3_1 =
13690  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
13691  sizeof(niFrameSurface1_t));
13692  niFrameSurface1_t *p_data3_2 =
13693  (niFrameSurface1_t *)((uint8_t *)p_frame->p_buffer +
13694  2 * sizeof(niFrameSurface1_t));
13695  // Libxcoder knows the handle so overwrite here
13696  p_data3->device_handle =
13697  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13698  p_data3_1->device_handle =
13699  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13700  p_data3_2->device_handle =
13701  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
13702  p_data3->ui16session_ID = p_data3_1->ui16session_ID =
13703  p_data3_2->ui16session_ID = (uint16_t)p_ctx->session_id;
13704  p_data3->src_cpu = p_data3_1->src_cpu = p_data3_2->src_cpu =
13705  (uint8_t)NI_DEVICE_TYPE_DECODER;
13706 
13707  p_data3->output_idx = 0;
13708  p_data3_1->output_idx = 1;
13709  p_data3_2->output_idx = 2;
13710 
13711  ni_log2(p_ctx, NI_LOG_DEBUG,
13712  "p_data3_1:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
13713  sei_size, p_data3_1->device_handle, p_ctx->hw_id,
13714  p_data3_1->ui16session_ID);
13715  ni_log2(p_ctx, NI_LOG_DEBUG,
13716  "p_data3_1: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
13717  p_data3_1->ui16FrameIdx, p_data3_1->ui32nodeAddress,
13718  p_data3_1->encoding_type, p_data3_1->bit_depth);
13719  ni_log2(p_ctx,
13720  NI_LOG_DEBUG,
13721  "p_data3_2:sei_size=%d device_handle=%d == hw_id=%d ses_id=%d\n",
13722  sei_size, p_data3_2->device_handle, p_ctx->hw_id,
13723  p_data3_2->ui16session_ID);
13724  ni_log2(p_ctx, NI_LOG_DEBUG,
13725  "p_data3_2: ui16FrameIdx=%d NodeAddre=0x%x planar=%d bd=%d\n",
13726  p_data3_2->ui16FrameIdx, p_data3_2->ui32nodeAddress,
13727  p_data3_2->encoding_type, p_data3_2->bit_depth);
13728 
13729  ni_log2(p_ctx, NI_LOG_DEBUG,
13730  "%s:sei_size=%d device_handle=%d == hw_id=%d "
13731  "ses_id=%d\n",
13732  __func__, sei_size, p_data3->device_handle, p_ctx->hw_id,
13733  p_data3->ui16session_ID);
13734  ni_log2(p_ctx, NI_LOG_DEBUG,
13735  "%s: session=0x%x ui16FrameIdx=%u NodeAddress=0x%x, "
13736  "planar=%d bd=%d\n",
13737  __func__, p_ctx->session_id, p_data3->ui16FrameIdx, p_data3->ui32nodeAddress,
13738  p_data3->encoding_type, p_data3->bit_depth);
13739  } else if (ni_cmp_fw_api_ver(
13741  "6rE") >= 0)
13742  {
13743  p_meta =
13744  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer);
13745  ni_log2(p_ctx, NI_LOG_DEBUG, "Got pkt_delay_cnt %u\n",
13746  p_meta->metadata_common.pkt_delay_cnt);
13747  if (p_ctx->pkt_delay_cnt < p_meta->metadata_common.pkt_delay_cnt)
13748  p_ctx->pkt_delay_cnt = p_meta->metadata_common.pkt_delay_cnt;
13749  }
13750 
13751  total_bytes_to_read = total_bytes_to_read + sei_size;
13752  ni_log2(p_ctx, NI_LOG_DEBUG,
13753  "%s decoder read desc success, retval %d "
13754  "total_bytes_to_read include sei %u sei_size %d\n",
13755  __func__, retval, total_bytes_to_read, sei_size);
13756 
13757  if (total_bytes_to_read > NI_MEM_PAGE_ALIGNMENT)
13758  {
13759  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Oversized metadata!\n", __func__);
13760  retval = NI_RETCODE_ERROR_MEM_ALOC;
13761  LRETURN;
13762  }
13763  }
13764 
13765  //bytes_read_so_far = total_bytes_to_read;
13766  // Note: session status is NOT reset but tracked between send
13767  // and recv to catch and recover from a loop condition
13768  //total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + p_frame->data_len[3] + metadata_hdr_size + sei_size; //since only HW desc
13769  bytes_read_so_far = total_bytes_to_read;
13770  //bytes_read_so_far = p_frame->data_len[0] + p_frame->data_len[1] + p_frame->data_len[2] + p_frame->data_len[3] + metadata_hdr_size + sei_size; //since only HW desc
13771  rx_size = ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, true);
13772  p_ctx->frame_pkt_offset = frame_offset;
13773  if (p_ctx->decoder_low_delay > 0 && buf_info.buf_avail_size == metadata_hdr_size &&
13774  p_ctx->enable_low_delay_check)
13775  {
13776  ni_log2(p_ctx, NI_LOG_TRACE, "Low delay mode amd check header if has b frame\n");
13777 
13778  ni_metadata_dec_frame_t *p_meta =
13779  (ni_metadata_dec_frame_t *)((uint8_t *)p_frame->p_buffer +
13780  p_frame->data_len[0] +
13781  p_frame->data_len[1] +
13782  p_frame->data_len[2]);
13783  if (p_meta->metadata_common.has_b_frame == 1)
13784  {
13785  ni_log2(p_ctx, NI_LOG_ERROR,"Warning: session 0x%x decoder lowDelay mode "
13786  "is cancelled due to has_b_frames, frame_num %u\n",
13787  p_ctx->session_id, p_ctx->frame_num);
13788  p_ctx->decoder_low_delay = 0;
13789  }
13790  }
13791 
13792  if (rx_size > 0)
13793  {
13794  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): s-state %d first_frame %d\n", __func__,
13795  p_ctx->session_run_state, p_ctx->is_first_frame);
13797  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
13799  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
13800  {
13801  if (p_ctx->last_dts != NI_NOPTS_VALUE && !p_ctx->ready_to_close)
13802  {
13803  p_ctx->pic_reorder_delay++;
13804  p_frame->dts = p_ctx->last_dts + p_ctx->last_dts_interval;
13805  ni_log2(p_ctx, NI_LOG_DEBUG, "Padding DTS: %" PRId64 "\n", p_frame->dts);
13806  } else
13807  {
13808  p_frame->dts = NI_NOPTS_VALUE;
13809  }
13810  }
13811 
13812  if (p_ctx->is_first_frame)
13813  {
13814  for (i = 0; i < p_ctx->pic_reorder_delay; i++)
13815  {
13816  if (p_ctx->last_pts == NI_NOPTS_VALUE &&
13817  p_ctx->last_dts == NI_NOPTS_VALUE)
13818  {
13819  // If the p_frame->pts is unknown in the very beginning we assume
13820  // p_frame->pts == 0 as well as DTS less than PTS by 1000 * 1/timebase
13821  if (p_frame->pts >= p_frame->dts &&
13822  p_frame->pts - p_frame->dts < 1000)
13823  {
13824  break;
13825  }
13826  }
13827 
13829  p_ctx->dts_queue, 0, (int64_t *)&p_frame->dts,
13831  p_ctx->frame_num % 500 == 0,
13832  p_ctx->buffer_pool) != NI_RETCODE_SUCCESS)
13833  {
13834  p_frame->dts = NI_NOPTS_VALUE;
13835  }
13836  }
13837  // Reset for DTS padding counting
13838  p_ctx->pic_reorder_delay = 0;
13839  }
13840  if (p_ctx->codec_format == NI_CODEC_FORMAT_JPEG)//fw won't save frameoffset when decoding jpeg.
13841  {
13842  if (p_ctx->is_first_frame)
13843  {
13844  p_ctx->is_first_frame = 0;
13845  }
13846  p_frame->pts = p_ctx->pts_offsets[p_ctx->frame_num % NI_FIFO_SZ];
13847  p_frame->flags = p_ctx->flags_array[p_ctx->frame_num % NI_FIFO_SZ];
13848  p_frame->pkt_pos = p_ctx->pkt_pos[p_ctx->frame_num % NI_FIFO_SZ];
13849  ni_log2(p_ctx, NI_LOG_DEBUG, "p_frame->pts = %u, frame_num = %d, p_frame->dts = %u\n",
13850  p_frame->pts, p_ctx->frame_num, p_frame->dts);
13851  }
13852  else if (p_ctx->is_dec_pkt_512_aligned)
13853  {
13854  if (p_ctx->is_first_frame)
13855  {
13856  p_ctx->is_first_frame = 0;
13857  p_frame->pkt_pos = p_ctx->pkt_pos[0];
13858 
13859  if (p_frame->dts == NI_NOPTS_VALUE)
13860  {
13861  p_frame->pts = NI_NOPTS_VALUE;
13862  }
13863  // if not a bitstream retrieve the pts of the frame corresponding to the first YUV output
13864  else if((p_ctx->pts_offsets[0] != NI_NOPTS_VALUE) && (p_ctx->pkt_index != -1))
13865  {
13866  ni_metadata_dec_frame_t* p_metadata =
13867  (ni_metadata_dec_frame_t*)((uint8_t*)p_frame->p_buffer +
13868  p_frame->data_len[0] + p_frame->data_len[1] +
13869  p_frame->data_len[2] + p_frame->data_len[3]);
13870  int num_fw_pkts =
13871  (int)p_metadata->metadata_common.ui64_data.frame_offset / 512;
13872  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: num_fw_pkts %d frame_offset %" PRIu64 "\n",
13873  __func__, num_fw_pkts,
13874  p_metadata->metadata_common.ui64_data.frame_offset);
13875  int idx = 0;
13876  uint64_t cumul = p_ctx->pkt_offsets_index[0];
13877  bool bFound = (num_fw_pkts >= cumul);
13878  while (cumul < num_fw_pkts) // look for pts index
13879  {
13880  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: cumul %" PRIu64 "\n", __func__, cumul);
13881  if (idx == NI_MAX_DEC_REJECT)
13882  {
13883  ni_log2(p_ctx, NI_LOG_ERROR,
13884  "Invalid index computation > "
13885  "NI_MAX_DEC_REJECT!\n");
13886  break;
13887  } else
13888  {
13889  idx ++;
13890  cumul += p_ctx->pkt_offsets_index[idx];
13891  ni_log2(p_ctx, NI_LOG_DEBUG,
13892  "%s: idx %d pkt_offsets_index[idx] %" PRIu64 "\n",
13893  __func__, idx, p_ctx->pkt_offsets_index[idx]);
13894  }
13895  }
13896  //if ((idx != NI_MAX_DEC_REJECT) && (idx >= 0))
13897  if ((idx != NI_MAX_DEC_REJECT) && bFound)
13898  {
13899  p_frame->pts = p_ctx->pts_offsets[idx];
13900  p_frame->flags = p_ctx->flags_array[idx];
13901  ni_log2(p_ctx, NI_LOG_DEBUG,
13902  "%s: (first frame) idx %d last_dts %" PRId64 ""
13903  " dts %" PRId64 " last_pts %" PRId64 " pts %" PRId64 "\n",
13904  __func__, idx, p_ctx->last_dts, p_frame->dts,
13905  p_ctx->last_pts, p_frame->pts);
13906  } else if (idx != NI_MAX_DEC_REJECT &&
13908  {
13909  ni_log2(p_ctx, NI_LOG_DEBUG,
13910  "%s(): session %u recovering and "
13911  "adjusting ts.\n",
13912  __func__, p_ctx->session_id);
13913  p_frame->pts = p_ctx->pts_offsets[idx];
13914  p_frame->flags = p_ctx->flags_array[idx];
13916  }
13917  else // use pts = 0 as offset
13918  {
13919  p_frame->pts = 0;
13920  ni_log2(p_ctx, NI_LOG_DEBUG,
13921  "%s: (zero default) dts %" PRId64 " pts "
13922  "%" PRId64 "\n",
13923  __func__, p_frame->dts, p_frame->pts);
13924  }
13925  }
13926  else
13927  {
13928  p_frame->pts = 0;
13929  ni_log2(p_ctx, NI_LOG_DEBUG,
13930  "%s: (not bitstream) dts %" PRId64 " pts "
13931  "%" PRId64 "\n",
13932  __func__, p_frame->dts, p_frame->pts);
13933  }
13934  }
13935  else
13936  {
13937  int64_t pts_delta = p_frame->dts - p_ctx->last_dts;
13938  p_frame->pts = p_ctx->last_pts + pts_delta;
13939  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
13940 
13941  ni_log2(p_ctx, NI_LOG_DEBUG,
13942  "%s: (!is_first_frame idx) last_dts %" PRId64 ""
13943  " dts %" PRId64 " pts_delta %" PRId64 " last_pts "
13944  "%" PRId64 " pts %" PRId64 "\n",
13945  __func__, p_ctx->last_dts, p_frame->dts, pts_delta,
13946  p_ctx->last_pts, p_frame->pts);
13947  }
13948  }
13949  else
13950  {
13951  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: frame_offset %" PRIu64 "\n", __func__,
13952  frame_offset);
13953  if (p_ctx->is_first_frame)
13954  {
13955  p_ctx->is_first_frame = 0;
13956  }
13957  // search for the pkt_offsets of received frame according to frame_offset.
13958  // here we get the index(i) which promises (p_ctx->pkt_offsets_index_min[i] <= frame_offset && p_ctx->pkt_offsets_index[i] > frame_offset)
13959  // i = -1 if not found
13960  i = rotated_array_binary_search(p_ctx->pkt_offsets_index_min,
13961  p_ctx->pkt_offsets_index, NI_FIFO_SZ,
13962  frame_offset);
13963  if (i >= 0)
13964  {
13965  p_frame->pts = p_ctx->pts_offsets[i];
13966  p_frame->flags = p_ctx->flags_array[i];
13967  p_frame->pkt_pos = p_ctx->pkt_pos[i];
13968  ni_log2(p_ctx, NI_LOG_DEBUG,
13969  "%s: (found pts) dts %" PRId64 " pts "
13970  "%" PRId64 " frame_offset %" PRIu64 " i %d "
13971  "pkt_offsets_index_min %" PRIu64 " "
13972  "pkt_offsets_index %" PRIu64 " pkt_pos %" PRIu64 "\n",
13973  __func__, p_frame->dts, p_frame->pts, frame_offset, i,
13974  p_ctx->pkt_offsets_index_min[i],
13975  p_ctx->pkt_offsets_index[i],
13976  p_ctx->pkt_pos[i]);
13977 
13978  p_frame->p_custom_sei_set = p_ctx->pkt_custom_sei_set[i];
13979  p_ctx->pkt_custom_sei_set[i] = NULL;
13980  } else
13981  {
13982  //backup solution pts
13983  if (p_param->dec_input_params.skip_pts_guess && p_ctx->last_pts != NI_NOPTS_VALUE)
13984  {
13985  // if skip guess_correct_pts, use pts interval to get the correct pts
13986  p_frame->pts = p_ctx->last_pts + (p_ctx->last_pts_interval > 0 ? p_ctx->last_pts_interval : 1);
13987  }
13988  else
13989  {
13990  p_frame->pts = p_ctx->last_pts + (p_frame->dts - p_ctx->last_dts);
13991  }
13992  p_frame->pkt_pos = p_ctx->last_pkt_pos + (frame_offset - p_ctx->last_frame_offset);
13993  ni_log2(p_ctx, NI_LOG_ERROR,
13994  "ERROR: Frame pts %" PRId64 " not found for offset "
13995  "%" PRIu64 "\n", p_frame->pts, frame_offset);
13996  ni_log2(p_ctx, NI_LOG_DEBUG,
13997  "%s: (not found use default) dts %" PRId64 " pts %" PRId64
13998  "\n",
13999  __func__, p_frame->dts, p_frame->pts);
14000  }
14001  }
14002 
14003  p_frame->orignal_pts = p_frame->pts;
14004  p_ctx->last_pkt_pos = p_frame->pkt_pos;
14005  p_ctx->last_frame_offset = frame_offset;
14006  if (!p_param->dec_input_params.skip_pts_guess)
14007  p_frame->pts = guess_correct_pts(p_ctx, p_frame->pts, p_frame->dts);
14008  if (p_frame->pts != NI_NOPTS_VALUE && p_ctx->last_pts != NI_NOPTS_VALUE)
14009  p_ctx->last_pts_interval = p_frame->pts - p_ctx->last_pts;
14010  p_ctx->last_pts = p_frame->pts;
14011  if (p_frame->dts != NI_NOPTS_VALUE && p_ctx->last_dts != NI_NOPTS_VALUE)
14012  p_ctx->last_dts_interval = p_frame->dts - p_ctx->last_dts;
14013  p_ctx->last_dts = p_frame->dts;
14014  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: (best_effort_timestamp) pts %" PRId64 "\n",
14015  __func__, p_frame->pts);
14016  p_ctx->frame_num++;
14017 
14018 #ifdef MEASURE_LATENCY
14019 #ifndef XCODER_311
14020  ni_log2(p_ctx, NI_LOG_INFO, "DEC pkt_num %d, fme_num %d, latecy is %d\n",
14021  p_ctx->pkt_num, p_ctx->frame_num, p_ctx->pkt_num - p_ctx->frame_num);
14022 #endif
14023 #endif
14024 
14025  }
14026 
14027  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n", __func__, rx_size);
14028  ni_log2(p_ctx, NI_LOG_DEBUG,
14029  "%s(): p_frame->start_of_stream=%u, "
14030  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
14031  "p_frame->video_height=%u\n",
14032  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
14033  p_frame->video_width, p_frame->video_height);
14034  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): p_frame->data_len[0/1/2]=%u/%u/%u\n", __func__,
14035  p_frame->data_len[0], p_frame->data_len[1], p_frame->data_len[2]);
14036 
14037  if (p_ctx->frame_num % 500 == 0)
14038  {
14039  ni_log2(p_ctx, NI_LOG_DEBUG,
14040  "Decoder pts queue size = %d dts queue size = %d\n\n",
14041  p_ctx->pts_table->list.count, p_ctx->dts_queue->list.count);
14042  // scan and clean up
14044  p_ctx->buffer_pool);
14045  }
14046 
14047 #ifdef MEASURE_LATENCY
14048  if ((p_frame->dts != NI_NOPTS_VALUE) && (p_ctx->frame_time_q != NULL))
14049  {
14050  uint64_t abs_time_ns = ni_gettime_ns();
14052  ni_log2(p_ctx, NI_LOG_INFO, "DTS:%" PRId64 ",DELTA:%" PRId64 ",dLAT:%" PRIu64 ";\n",
14053  p_frame->dts, abs_time_ns - q->last_benchmark_time,
14054  ni_lat_meas_q_check_latency(q, abs_time_ns, p_frame->dts));
14055  q->last_benchmark_time = abs_time_ns;
14056  }
14057 #endif
14058 
14059 END:
14060 
14061  ni_pthread_mutex_unlock(&p_ctx->mutex);
14062 
14063  if (get_first_metadata && p_data_buffer)
14064  ni_aligned_free(p_data_buffer);
14065  if (sequence_change && p_ctx->frame_num)
14066  {
14067  if (p_ctx->actual_video_width == p_frame->video_width &&
14068  p_ctx->active_video_height == p_frame->video_height)
14069  {
14070  p_ctx->pixel_format_changed = 1;
14071  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): format changed\n", __func__);
14072  }
14073  }
14074  if (NI_RETCODE_SUCCESS != retval)
14075  {
14076  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): bad exit, retval = %d\n", __func__, retval);
14077  if (retval == NI_RETCODE_ERROR_VPU_RECOVERY)
14078  {
14079  low_delay_signal(p_ctx);
14080  }
14081 
14082  return retval;
14083  } else
14084  {
14085  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
14086  if (low_delay_notify)
14087  {
14088  low_delay_signal(p_ctx);
14089  }
14090 
14091  return rx_size;
14092  }
14093 }
14094 
14095 /*!******************************************************************************
14096 * \brief Retrieve a YUV p_frame from decoder
14097 *
14098 * \param
14099 *
14100 * \return
14101 *******************************************************************************/
14103 {
14104  int retval = NI_RETCODE_SUCCESS;
14105  int rx_size = 0;
14106  uint64_t frame_offset = 0;
14107  uint8_t *p_data_buffer;
14108  int metadata_hdr_size = NI_FW_META_DATA_SZ -
14110  uint32_t total_bytes_to_read = 0;
14111  uint32_t read_size_bytes = 0;
14112  uint32_t ui32LBA = 0;
14113 
14114  //ni_log2(p_ctx, NI_LOG_DEBUG, "hwcontext.c:ni_hwdl_frame() hwdesc %d %d %d\n",
14115  // hwdesc->ui16FrameIdx,
14116  // hwdesc->i8InstID,
14117  // hwdesc->ui16session_ID);
14118 
14119  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14120 
14121  if ((!p_ctx) || (!p_frame))
14122  {
14123  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14124  __func__);
14125  return NI_RETCODE_INVALID_PARAM;
14126  }
14127 
14128  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14129  {
14130  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14131  __func__);
14133  LRETURN;
14134  }
14135 
14136  p_data_buffer = (uint8_t *)p_frame->p_buffer;
14137 
14138  if (!p_frame->p_data[0] || !p_data_buffer)
14139  {
14140  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): No receive buffer allocated.\n",
14141  __func__);
14142  retval = NI_RETCODE_INVALID_PARAM;
14143  LRETURN;
14144  }
14145 
14146  if (0 == p_frame->data_len[0])
14147  {
14148  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): p_frame->data_len[0] = 0!.\n",
14149  __func__);
14150  retval = NI_RETCODE_INVALID_PARAM;
14151  LRETURN;
14152  }
14153 
14155  {
14156  p_frame->data_len[2] = 0;
14157  } else if (hwdesc->encoding_type == NI_PIXEL_PLANAR_FORMAT_TILED4X4)
14158  {
14159  ni_log2(p_ctx, NI_LOG_ERROR,
14160  "ERROR %s(): NI_PIXEL_PLANAR_FORMAT_TILED4X4 not supported in download.\n",
14161  __func__);
14162  retval = NI_RETCODE_INVALID_PARAM;
14163  LRETURN;
14164  }
14165  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
14166  p_frame->data_len[2];// +metadata_hdr_size;
14167  unsigned int bytes_read_so_far = 0;
14168  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
14169  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
14170  ni_log2(p_ctx, NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
14171  "%u, minorOffset %u\n",
14172  total_bytes_to_read, hwdesc->ui32nodeAddress,
14173  output_chunk_offset, output_minor_offset);
14174 
14175  ni_log2(p_ctx, NI_LOG_DEBUG, "total_bytes_to_read %u max_nvme_io_size %u ylen %u cr len "
14176  "%u cb len %u hdr %d\n",
14177  total_bytes_to_read, p_ctx->max_nvme_io_size,
14178  p_frame->data_len[0], p_frame->data_len[1],
14179  p_frame->data_len[2], metadata_hdr_size);
14180 
14181  //Apply read configuration here
14182  retval =
14184  (output_minor_offset << NI_CODEC_HW_PAYLOAD_OFFSET) |
14186  hwdesc->ui16FrameIdx);
14187  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
14188  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14189  CHECK_VPU_RECOVERY(retval);
14190  if (retval < 0)
14191  {
14192  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14194  LRETURN;
14195  } else
14196  {
14197  ni_log2(p_ctx, NI_LOG_DEBUG,
14198  "Config HW download read desc success, retval %d total_bytes_to_read %u\n",
14199  retval, total_bytes_to_read);
14200  }
14201 
14202  read_size_bytes = total_bytes_to_read;
14203  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, hwdesc->src_cpu);
14204  ui32LBA += output_chunk_offset;
14205  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
14206  {
14207  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
14208  }
14209 
14210  retval = ni_nvme_send_read_cmd(
14211  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
14212  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
14213  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, hwdesc->src_cpu,
14214  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14215  CHECK_VPU_RECOVERY(retval);
14216  if (retval < 0)
14217  {
14218  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14220  LRETURN;
14221  } else
14222  {
14223  ni_log2(p_ctx, NI_LOG_DEBUG,
14224  "HW download read desc success, retval %d total_bytes_to_read %u\n",
14225  retval, total_bytes_to_read);
14226  }
14227 
14228  //Unset applied read configuration here
14229  retval = ni_config_session_rw(p_ctx, SESSION_READ_CONFIG, 0, 0, 0);
14230  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config, hwdesc->src_cpu,
14231  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14232  CHECK_VPU_RECOVERY(retval);
14233 
14234  if (retval < 0)
14235  {
14236  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14238  LRETURN;
14239  } else
14240  {
14241  ni_log2(p_ctx, NI_LOG_DEBUG,
14242  "Unconfig HW download read desc success, retval %d total_bytes_to_read %u\n",
14243  retval, total_bytes_to_read);
14244  }
14245 
14246  bytes_read_so_far = total_bytes_to_read;
14247  // Note: session status is NOT reset but tracked between send
14248  // and recv to catch and recover from a loop condition
14249 
14250  if (p_ctx->is_auto_dl)
14251  rx_size = (int)bytes_read_so_far;
14252  else
14253  {
14254  rx_size =
14255  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
14256  p_ctx->frame_pkt_offset = frame_offset;
14257  }
14258 
14259  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): received data: [0x%08x]\n",
14260  __func__, rx_size);
14261  ni_log2(p_ctx, NI_LOG_DEBUG,
14262  "%s(): p_frame->start_of_stream=%u, "
14263  "p_frame->end_of_stream=%u, p_frame->video_width=%u, "
14264  "p_frame->video_height=%u\n",
14265  __func__, p_frame->start_of_stream, p_frame->end_of_stream,
14266  p_frame->video_width, p_frame->video_height);
14267  ni_log2(p_ctx, NI_LOG_DEBUG,
14268  "%s(): p_ctx->frame_num %" PRIu64 ", "
14269  "p_frame->data_len[0/1/2]=%u/%u/%u\n",
14270  __func__, p_ctx->frame_num, p_frame->data_len[0], p_frame->data_len[1],
14271  p_frame->data_len[2]);
14272 
14273  //if (decq_count % 500 == 0)
14274  //{
14275  // ni_log2(p_ctx, NI_LOG_DEBUG, "Decoder pts queue size = %d dts queue size = %d\n\n",
14276  // p_ctx->pts_table->list.count, p_ctx->dts_queue)->list.count);
14277  // // scan and clean up
14278  // ni_timestamp_scan_cleanup(p_ctx->pts_table, p_ctx->dts_queue, p_ctx->buffer_pool);
14279  //}
14280 
14281 END:
14282 
14283  if (NI_RETCODE_SUCCESS != retval)
14284  {
14285  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
14286  return retval;
14287  } else
14288  {
14289  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
14290  return rx_size;
14291  }
14292 }
14293 
14294 int ni_hwdownload_by_frame_idx(niFrameSurface1_t* hwdesc, ni_frame_t* p_frame, int is_auto_dl)
14295 {
14296  int retval = NI_RETCODE_SUCCESS;
14297  int rx_size = 0;
14298  uint64_t frame_offset = 0;
14299  uint8_t *p_data_buffer;
14300  int metadata_hdr_size = NI_FW_META_DATA_SZ -
14302  uint32_t total_bytes_to_read = 0;
14303  uint32_t read_size_bytes = 0;
14304  uint32_t ui32LBA = 0;
14305  ni_global_session_stats_t *global_state;
14306  const char *error_flag = "NetintQuadraErr";
14307 
14308  ni_log(NI_LOG_TRACE, "%s(): enter\n", __func__);
14309 
14310  p_data_buffer = (uint8_t *)p_frame->p_buffer;
14311 
14312  if (!p_frame->p_data[0] || !p_data_buffer || 0 == p_frame->data_len[0])
14313  {
14314  ni_log(NI_LOG_ERROR, "ERROR %s(): passed parameters are null or p_frame->data_len is 0.\n",
14315  __func__);
14316  retval = NI_RETCODE_INVALID_PARAM;
14317  LRETURN;
14318  }
14319 
14321  {
14322  p_frame->data_len[2] = 0;
14323  } else if (hwdesc->encoding_type >= NI_PIXEL_PLANAR_FORMAT_TILED4X4)
14324  {
14326  "ERROR %s(): Unsupported format %d for hw download.\n",
14327  __func__, hwdesc->encoding_type);
14328  retval = NI_RETCODE_INVALID_PARAM;
14329  LRETURN;
14330  }
14331  total_bytes_to_read = p_frame->data_len[0] + p_frame->data_len[1] +
14332  p_frame->data_len[2];// +metadata_hdr_size;
14333  unsigned int bytes_read_so_far = 0;
14334  uint32_t output_chunk_offset = hwdesc->ui32nodeAddress / FRAME_CHUNK_INDEX_SIZE; //for reading output1 or output2
14335  uint32_t output_minor_offset = hwdesc->ui32nodeAddress - output_chunk_offset * FRAME_CHUNK_INDEX_SIZE;
14336 
14337  ni_log(NI_LOG_DEBUG, "total_bytes_to_read %u ylen %u cr len "
14338  "%u cb len %u hdr %d\n",
14339  total_bytes_to_read,
14340  p_frame->data_len[0], p_frame->data_len[1],
14341  p_frame->data_len[2], metadata_hdr_size);
14342 
14343  // preset content of buffer, if not refresh means download fail
14344  memset(p_data_buffer, 0, NI_DATA_BUFFER_LEN);
14345  global_state = (ni_global_session_stats_t *)p_data_buffer;
14346  memcpy(global_state->error_flag, error_flag, strlen(error_flag)+1);
14347  global_state->check_flag[0] = global_state->check_flag[2] = 0x0;
14348  global_state->check_flag[1] = global_state->check_flag[3] = 0xFF;
14349 
14350  read_size_bytes = total_bytes_to_read;
14351  ui32LBA = DOWNLOAD_FRAMEIDX_R(hwdesc->ui16FrameIdx);
14352  ui32LBA += output_chunk_offset;
14353  if (read_size_bytes % NI_MEM_PAGE_ALIGNMENT)
14354  {
14355  read_size_bytes = ( (read_size_bytes / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT) + NI_MEM_PAGE_ALIGNMENT;
14356  }
14357  ni_log(NI_LOG_DEBUG, "Total bytes to download %u, start offset = %u, chunkOffset "
14358  "%u, minorOffset %u hwdesc->ui16FrameIdx %d ui32LBA 0x%x\n",
14359  total_bytes_to_read, hwdesc->ui32nodeAddress,
14360  output_chunk_offset, output_minor_offset, hwdesc->ui16FrameIdx, ui32LBA);
14361 
14362  retval = ni_nvme_send_read_cmd(
14363  (ni_device_handle_t)(int64_t)hwdesc->device_handle,
14364  NI_INVALID_DEVICE_HANDLE, p_data_buffer, read_size_bytes, ui32LBA);
14365 
14366  if (retval < 0)
14367  {
14368  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
14370  LRETURN;
14371  }
14372  global_state = (ni_global_session_stats_t *)p_data_buffer;
14373  if (!strcmp(global_state->error_flag, error_flag) &&
14374  global_state->check_flag[0] == 0x0 && global_state->check_flag[1] == 0xFF &&
14375  global_state->check_flag[2] == 0x0 && global_state->check_flag[3] == 0xFF) {
14376  ni_log(NI_LOG_ERROR, "ERROR %s(): nvme download failed, invalid frameidx %u or "
14377  "size %u + offset %u out of range\n",
14378  __func__, hwdesc->ui16FrameIdx, read_size_bytes, output_chunk_offset);
14379  retval = NI_RETCODE_INVALID_PARAM;
14380  LRETURN;
14381  }
14382 
14383  bytes_read_so_far = total_bytes_to_read;
14384  // Note: session status is NOT reset but tracked between send
14385  // and recv to catch and recover from a loop condition
14386 
14387  if (is_auto_dl)
14388  rx_size = (int)bytes_read_so_far;
14389  else
14390  {
14391  rx_size =
14392  ni_create_frame(p_frame, bytes_read_so_far, &frame_offset, false);
14393  }
14394 
14396  "HW download read desc success, retval %d total_bytes_to_read %u\n",
14397  retval, total_bytes_to_read);
14398 
14399 END:
14400 
14401  if (NI_RETCODE_SUCCESS != retval)
14402  {
14403  ni_log(NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
14404  return retval;
14405  } else
14406  {
14407  ni_log(NI_LOG_TRACE, "%s(): exit, rx_size = %d\n", __func__, rx_size);
14408  return rx_size;
14409  }
14410 }
14411 
14413  ni_frameclone_desc_t *p_frameclone_desc)
14414 {
14415  int retval = NI_RETCODE_SUCCESS;
14416  uint8_t *p_data = NULL;
14417  uint32_t dataLen;
14418  uint32_t ui32LBA = 0;
14419 
14420  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14421 
14422  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14423  {
14424  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14425  __func__);
14427  LRETURN;
14428  }
14429 
14430  dataLen = (sizeof(ni_frameclone_desc_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
14431  ~(NI_MEM_PAGE_ALIGNMENT - 1);
14432  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
14433  {
14434  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
14435  NI_ERRNO, __func__);
14436  retval = NI_RETCODE_ERROR_MEM_ALOC;
14437  LRETURN;
14438  }
14439  memset(p_data, 0, dataLen);
14440  memcpy(p_data, p_frameclone_desc, sizeof(ni_frameclone_desc_t));
14441  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): ui16DstIdx %u, ui16SrcIdx %u, size %u, offset %u\n", __func__,
14442  p_frameclone_desc->ui16DstIdx, p_frameclone_desc->ui16SrcIdx,
14443  p_frameclone_desc->ui32Size, p_frameclone_desc->ui32Offset);
14444 
14445  ui32LBA = CONFIG_SESSION_FRAME_COPY_W(p_ctx->session_id);
14446  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
14447  p_data, dataLen, ui32LBA);
14448  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
14449  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
14450  OPT_1);
14451  CHECK_VPU_RECOVERY(retval);
14452  if (NI_RETCODE_SUCCESS != retval)
14453  {
14454  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
14455  __func__);
14457  LRETURN;
14458  }
14459 
14460 END:
14461  ni_aligned_free(p_data);
14462  if (NI_RETCODE_SUCCESS != retval)
14463  {
14464  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): bad exit, retval = %d\n", __func__, retval);
14465  }
14466  return retval;
14467 }
14468 
14469 /*!******************************************************************************
14470 * \brief Close an xcoder upload instance
14471 *
14472 * \param p_ctx pointer to uploader session context
14473 *
14474 * \return NI_RETCODE_SUCCESS
14475 *******************************************************************************/
14477 {
14478 #ifndef _WIN32
14479  if (p_ctx->isP2P)
14480  {
14481  if (p_ctx->netint_fd)
14482  {
14483  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: close driver fd %d\n", __func__,
14484  p_ctx->netint_fd);
14485  close(p_ctx->netint_fd);
14486  }
14487  }
14488 #endif
14489  return NI_RETCODE_SUCCESS;
14490 }
14491 
14492 /*!******************************************************************************
14493 * \brief Send a p_config command to configure uploading parameters.
14494 *
14495 * \param ni_session_context_t p_ctx - xcoder Context
14496 * \param[in] pool_size pool size to create
14497 * \param[in] pool 0 = normal pool, 1 = P2P pool
14498 *
14499 * \return - NI_RETCODE_SUCCESS on success,
14500 * NI_RETCODE_ERROR_INVALID_SESSION
14501 * NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
14502 *******************************************************************************/
14504  uint32_t pool_size,
14505  uint32_t pool)
14506 {
14507  void* p_uploader_config = NULL;
14508  ni_uploader_config_t* p_cfg = NULL;
14509  uint32_t buffer_size = sizeof(ni_encoder_config_t);
14511  uint32_t ui32LBA = 0;
14512  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14513 
14514  if (!p_ctx)
14515  {
14516  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14517  __func__);
14518  retval = NI_RETCODE_INVALID_PARAM;
14519  LRETURN;
14520  }
14521 
14522  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14523  {
14524  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14525  __func__);
14527  LRETURN;
14528  }
14529 
14530  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
14531  if (ni_posix_memalign(&p_uploader_config, sysconf(_SC_PAGESIZE),
14532  buffer_size))
14533  {
14534  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
14535  NI_ERRNO, __func__);
14536  retval = NI_RETCODE_ERROR_MEM_ALOC;
14537  LRETURN;
14538  }
14539  memset(p_uploader_config, 0, buffer_size);//
14540 
14541  p_cfg = (ni_uploader_config_t*)p_uploader_config;
14542  p_cfg->ui16picWidth = p_ctx->active_video_width;
14543  p_cfg->ui16picHeight = p_ctx->active_video_height;
14544  p_cfg->ui8poolSize = pool_size;
14545  p_cfg->ui8PixelFormat = p_ctx->pixel_format;
14546  p_cfg->ui8Pool = pool;
14547  p_cfg->ui32lumaLinesize = 0; // cannot support config linesize yet because ni_frames_init is called when filter graph is created (no frame linesize info)
14548  p_cfg->ui32chromaLinesize = 0;
14549  bool isrgb = ((NI_PIX_FMT_RGBA == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_BGRA == p_cfg->ui8PixelFormat) ||
14550  (NI_PIX_FMT_ARGB == p_cfg->ui8PixelFormat) || (NI_PIX_FMT_ABGR == p_cfg->ui8PixelFormat) ||
14551  (NI_PIX_FMT_BGR0 == p_cfg->ui8PixelFormat));
14555  {
14556  ni_log2(p_ctx, NI_LOG_ERROR, "Resolution %d x %d not supported for %d format!\n", p_cfg->ui16picWidth, p_cfg->ui16picHeight, p_cfg->ui8PixelFormat);
14557  retval = NI_RETCODE_INVALID_PARAM;
14558  LRETURN;
14559  }
14560 
14561  ni_log2(p_ctx, NI_LOG_DEBUG, "ni_config_instance_set_uploader_params():%d x %d x Format %d with %d framepool\n",
14562  p_cfg->ui16picWidth, p_cfg->ui16picHeight, p_cfg->ui8PixelFormat, p_cfg->ui8poolSize);
14563 
14564  //configure the session here
14565  ui32LBA =
14567 
14568  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14569  p_uploader_config, buffer_size, ui32LBA);
14570  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
14571  p_ctx->device_type, p_ctx->hw_id,
14572  &(p_ctx->session_id), OPT_1);
14573  if (NI_RETCODE_SUCCESS != retval)
14574  {
14575  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
14576  //Close the session since we can't configure it
14577  retval = ni_encoder_session_close(p_ctx, 0);
14578  if (NI_RETCODE_SUCCESS != retval)
14579  {
14580  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_uploader_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
14581  }
14582 
14584  }
14585  else
14586  {
14588  }
14589 
14590 END:
14591 
14592  ni_aligned_free(p_uploader_config);
14593  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
14594 
14595  return retval;
14596 }
14597 
14598 /*!******************************************************************************
14599 * \brief Send a p_config command to configure decoding parameters.
14600 *
14601 * \param ni_session_context_t p_ctx - xcoder Context
14602 * \param uint32_t max_pkt_size - overwrite maximum packet size if nonzero
14603 *
14604 * \return - NI_RETCODE_SUCCESS on success, NI_RETCODE_ERROR_INVALID_SESSION, NI_RETCODE_ERROR_NVME_CMD_FAILED on failure
14605 *******************************************************************************/
14607 {
14608  void* p_decoder_config = NULL;
14609  uint32_t buffer_size = sizeof(ni_decoder_config_t);
14611  uint32_t ui32LBA = 0;
14612  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14613 
14614  if (!p_ctx)
14615  {
14616  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14617  __func__);
14618  retval = NI_RETCODE_INVALID_PARAM;
14619  LRETURN;
14620  }
14621 
14622  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14623  {
14624  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14625  __func__);
14627  LRETURN;
14628  }
14629 
14630  if (ni_cmp_fw_api_ver((char*)&p_ctx->fw_rev[NI_XCODER_REVISION_API_MAJOR_VER_IDX], "6rT") < 0)
14631  {
14632  ni_log2(p_ctx, NI_LOG_INFO, "%s() FW rev %s < 6rT-- load balancing might be affected\n", __func__,
14634  }
14635 
14636  buffer_size = ((buffer_size + (NI_MEM_PAGE_ALIGNMENT - 1)) / NI_MEM_PAGE_ALIGNMENT) * NI_MEM_PAGE_ALIGNMENT;
14637  if (ni_posix_memalign(&p_decoder_config, sysconf(_SC_PAGESIZE), buffer_size))
14638  {
14639  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate p_decoder_config buffer\n",
14640  NI_ERRNO, __func__);
14641  retval = NI_RETCODE_ERROR_MEM_ALOC;
14642  LRETURN;
14643  }
14644  memset(p_decoder_config, 0, buffer_size);
14645 
14646  ni_set_custom_dec_template(p_ctx, p_decoder_config, p_ctx->p_session_config, max_pkt_size);
14647  retval = ni_validate_custom_dec_template(p_ctx->p_session_config, p_ctx, p_decoder_config, p_ctx->param_err_msg, sizeof(p_ctx->param_err_msg));
14648  if (NI_RETCODE_PARAM_WARN == retval)
14649  {
14650  ni_log2(p_ctx, NI_LOG_INFO, "WARNING: %s . %s\n", __func__, p_ctx->param_err_msg);
14651  fflush(stdout);
14652  }
14653  else if (NI_RETCODE_SUCCESS != retval)
14654  {
14655  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s failed. %s\n", __func__,
14656  p_ctx->param_err_msg);
14657  fflush(stdout);
14658  retval = NI_RETCODE_INVALID_PARAM;
14659  LRETURN;
14660  }
14661 
14662  // configure the session here
14664 
14665  //Flip the bytes!!
14666  //p_cfg = (ni_decoder_config_t*)p_decoder_config;
14667  //p_cfg->i32picWidth = ni_htonl(p_cfg->i32picWidth);
14668  //p_cfg->i32picHeight = ni_htonl(p_cfg->i32picHeight);
14669  //p_cfg->i32meBlkMode = ni_htonl(p_cfg->i32meBlkMode);
14670  //p_cfg->i32frameRateInfo = ni_htonl(p_cfg->i32frameRateInfo);
14671  //p_cfg->i32vbvBufferSize = ni_htonl(p_cfg->i32vbvBufferSize);
14672  //p_cfg->i32userQpMax = ni_htonl(p_cfg->i32userQpMax);
14673  //p_cfg->i32maxIntraSize = ni_htonl(p_cfg->i32maxIntraSize);
14674  //p_cfg->i32userMaxDeltaQp = ni_htonl(p_cfg->i32userMaxDeltaQp);
14675  //p_cfg->i32userMinDeltaQp = ni_htonl(p_cfg->i32userMinDeltaQp);
14676  //p_cfg->i32userQpMin = ni_htonl(p_cfg->i32userQpMin);
14677  //p_cfg->i32bitRate = ni_htonl(p_cfg->i32bitRate);
14678  //p_cfg->i32bitRateBL = ni_htonl(p_cfg->i32bitRateBL);
14679  //p_cfg->i32srcBitDepth = ni_htonl(p_cfg->i32srcBitDepth);
14680  //p_cfg->hdrEnableVUI = ni_htonl(p_cfg->hdrEnableVUI);
14681  //p_cfg->ui32VuiDataSizeBits = ni_htonl(p_cfg->ui32VuiDataSizeBits);
14682  //p_cfg->ui32VuiDataSizeBytes = ni_htonl(p_cfg->ui32VuiDataSizeBytes);
14683  //p_cfg->i32hwframes = ni_htonl(p_cfg->i32hwframes);
14684  // flip the 16 bytes of the reserved field using 32 bits pointers
14685  //for (i = 0; i < (16 >> 2); i++)
14686  //{
14687  // ((uint32_t*)p_cfg->ui8Reserved)[i] = ni_htonl(((uint32_t*)p_cfg->ui8Reserved)[i]);
14688  //}
14690  //for (i = 0; i < (NI_MAX_VUI_SIZE >> 2); i++) // apply on 32 bits
14691  //{
14692  // ((uint32_t*)p_cfg->ui8VuiRbsp)[i] = ni_htonl(((uint32_t*)p_cfg->ui8VuiRbsp)[i]);
14693  //}
14694 
14695  ni_log2(p_ctx, NI_LOG_DEBUG, "%s: ui32LBA = 0x%x\n", __func__, ui32LBA);
14696  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14697  p_decoder_config, buffer_size, ui32LBA);
14698  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
14699  p_ctx->device_type, p_ctx->hw_id,
14700  &(p_ctx->session_id), OPT_1);
14701  if (NI_RETCODE_SUCCESS != retval)
14702  {
14703  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
14704  //Close the session since we can't configure it
14705  retval = ni_decoder_session_close(p_ctx, 0);
14706  if (NI_RETCODE_SUCCESS != retval)
14707  {
14708  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: ni_encoder_session_close failed: blk_io_handle: %" PRIx64 ", hw_id, %u, xcoder_inst_id: %d\n", (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
14709  }
14710 
14712  }
14713 
14714 END:
14715 
14716  ni_aligned_free(p_decoder_config);
14717  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
14718  return retval;
14719 }
14720 
14721 /*!******************************************************************************
14722  * \brief read a hardware descriptor from a scaler session
14723  *
14724  * \param[in] p_ctx pointer to session context
14725  * \param[out] p_frame pointer to frame to write hw descriptor
14726  *
14727  * \return NI_RETCODE_INVALID_PARAM
14728  * NI_RETCODE_ERROR_INVALID_SESSION
14729  * NI_RETCODE_ERROR_MEM_ALOC
14730  * NI_RETCODE_ERROR_NVME_CMD_FAILED
14731  * NI_RETCODE_FAILURE
14732  *******************************************************************************/
14734  ni_session_context_t *p_ctx,
14735  ni_frame_t *p_frame)
14736 {
14737  ni_retcode_t retval;
14738  ni_instance_buf_info_t sInstanceBuf = {0};
14739  niFrameSurface1_t *pFrameSurface;
14740  int query_retry = 0;
14741 
14742  if (!p_ctx)
14743  {
14744  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14745  __func__);
14746  return NI_RETCODE_INVALID_PARAM;
14747  }
14748 
14749  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
14750  {
14751  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14752  __func__);
14754  }
14755 
14756  ni_pthread_mutex_lock(&p_ctx->mutex);
14757 
14758  for (;;)
14759  {
14760  query_retry++;
14761 
14763  NI_DEVICE_TYPE_SCALER, &sInstanceBuf);
14764 
14765  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
14766  p_ctx->device_type, p_ctx->hw_id,
14767  &(p_ctx->session_id), OPT_3);
14768 
14770  {
14771  if (query_retry >= 1000)
14772  {
14773  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n", retval);
14774  LRETURN;
14775  }
14776  }
14777  else if (retval != NI_RETCODE_SUCCESS)
14778  {
14779  LRETURN;
14780  }
14781  else
14782  {
14783  pFrameSurface = (niFrameSurface1_t *) p_frame->p_data[3];
14784  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
14785  pFrameSurface->ui16session_ID = p_ctx->session_id;
14786  pFrameSurface->device_handle =
14787  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
14788  pFrameSurface->src_cpu = (uint8_t) NI_DEVICE_TYPE_SCALER;
14789  pFrameSurface->output_idx = 0;
14790 
14791  /* A frame index of zero is invalid, the memory acquisition failed */
14792  if (pFrameSurface->ui16FrameIdx == 0)
14793  {
14794  if (query_retry >= 1000)
14795  {
14796  ni_log2(p_ctx, NI_LOG_ERROR, "Warning: 2D could not acquire frame\n");
14797  retval = NI_RETCODE_FAILURE;
14798  LRETURN;
14799  }
14800  ni_usleep(100);
14801  continue;
14802  }
14803 
14804  ni_log2(p_ctx, NI_LOG_DEBUG,
14805  "Session=0x%x: %s got FrameIndex=%u\n",
14806  p_ctx->session_id,
14807  __func__,
14808  pFrameSurface->ui16FrameIdx);
14809 
14810  LRETURN;
14811  }
14812  }
14813 
14814 END:
14815 
14816  ni_pthread_mutex_unlock(&p_ctx->mutex);
14817 
14818  if (NI_RETCODE_SUCCESS != retval)
14819  {
14820  ni_log2(p_ctx, NI_LOG_DEBUG,
14821  "%s Warning scalar read hwdesc fail rc %d or ind !\n",
14822  __func__, retval);
14823  retval = NI_RETCODE_FAILURE;
14824  }
14825 
14826  return retval;
14827 }
14828 
14829 /*!******************************************************************************
14830 * \brief Grab bitdepth factor from NI_PIX_FMT
14831 *
14832 * \param[in] pix_fmt ni_pix_fmt_t
14833 *
14834 * \return 1 or 2 for success, -1 for error
14835 *******************************************************************************/
14837 {
14838  switch (pix_fmt)
14839  {
14840  case NI_PIX_FMT_YUV420P:
14841  case NI_PIX_FMT_NV12:
14842  case NI_PIX_FMT_8_TILED4X4:
14843  return 1;
14845  case NI_PIX_FMT_P010LE:
14847  return 2;
14848  default:
14849  {
14850  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
14851  pix_fmt);
14852  break;
14853  }
14854  }
14855  return -1;
14856 }
14857 
14858 /*!******************************************************************************
14859 * \brief Grab planar info from NI_PIX_FMT
14860 *
14861 * \param[in] pix_fmt ni_pix_fmt_t
14862 *
14863 * \return 0 or 1 for success, -1 for error
14864 *******************************************************************************/
14866 {
14867  switch (pix_fmt)
14868  {
14869  case NI_PIX_FMT_YUV420P:
14871  case NI_PIX_FMT_ABGR:
14872  case NI_PIX_FMT_ARGB:
14873  case NI_PIX_FMT_RGBA:
14874  case NI_PIX_FMT_BGRA:
14875  return 1;
14876  break;
14877  case NI_PIX_FMT_NV12:
14878  case NI_PIX_FMT_P010LE:
14879  return 0;
14880  break;
14881  default:
14882  {
14883  ni_log(NI_LOG_ERROR, "ERROR: %s() non applicable format %d\n", __func__,
14884  pix_fmt);
14885  break;
14886  }
14887  }
14888  return -1;
14889 }
14890 
14891 #ifndef _WIN32
14892 /*!*****************************************************************************
14893  * \brief Get an address offset from a hw descriptor
14894  *
14895  * \param[in] p_ctx ni_session_context_t to be referenced
14896  * \param[in] hwdesc Pointer to caller allocated niFrameSurface1_t
14897  * \param[out] p_offset Value of offset
14898  *
14899  * \return On success NI_RETCODE_SUCCESS
14900  * On failure NI_RETCODE_INVALID_PARAM
14901  ******************************************************************************/
14903  uint32_t *p_offset)
14904 {
14905  if (!hwdesc)
14906  {
14907  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed null parameter\n", __func__);
14908  return NI_RETCODE_INVALID_PARAM;
14909  }
14910 
14911  if (hwdesc->ui16FrameIdx <= NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config) ||
14913  {
14914  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() pass invalid data. "
14915  "FrameIdx %d OOR (%d,%d]. DDR config %d \n", __func__, hwdesc->ui16FrameIdx,
14917  p_ctx->ddr_config);
14918  return NI_RETCODE_INVALID_PARAM;
14919  }
14920 
14921  *p_offset = (hwdesc->ui16FrameIdx - NI_GET_MIN_HWDESC_P2P_BUF_ID(p_ctx->ddr_config)) *
14923 
14924  return NI_RETCODE_SUCCESS;
14925 }
14926 #endif
14927 
14928 /* AI functions */
14930  void *nb_data, uint32_t nb_size)
14931 {
14932  void *p_ai_config = NULL;
14933  void *p_nb_data = NULL;
14934  uint32_t buffer_size;
14935  // uint8_t *p_data;
14936  // uint32_t transferred, this_size;
14938  uint32_t ui32LBA = 0;
14939  uint32_t config_size;
14940  void *p_buffer = NULL;
14941  uint32_t dataLen;
14942  ni_instance_buf_info_t buf_info = {0};
14943  int retry_count = 0;
14944 
14945  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
14946 
14947  if (!p_ctx)
14948  {
14949  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
14950  __func__);
14951  retval = NI_RETCODE_INVALID_PARAM;
14952  LRETURN;
14953  }
14954 
14955  ni_pthread_mutex_lock(&p_ctx->mutex);
14956 
14957  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
14958  {
14959  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
14960  __func__);
14962  LRETURN;
14963  }
14964 
14965  config_size = (sizeof(ni_ai_config_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
14966  ~(NI_MEM_PAGE_ALIGNMENT - 1);
14967  if (ni_posix_memalign(&p_ai_config, sysconf(_SC_PAGESIZE), config_size))
14968  {
14969  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate ai config buffer.\n");
14970  retval = NI_RETCODE_ERROR_MEM_ALOC;
14971  LRETURN;
14972  }
14973 
14974  ((ni_ai_config_t *)p_ai_config)->ui32NetworkBinarySize = nb_size;
14975  ni_calculate_sha256(nb_data, nb_size,
14976  ((ni_ai_config_t *)p_ai_config)->ui8Sha256);
14977 
14978  buffer_size =
14979  (nb_size + (NI_MEM_PAGE_ALIGNMENT - 1)) & ~(NI_MEM_PAGE_ALIGNMENT - 1);
14980  if (ni_posix_memalign(&p_nb_data, sysconf(_SC_PAGESIZE), buffer_size))
14981  {
14982  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate encConf buffer.\n");
14983  retval = NI_RETCODE_ERROR_MEM_ALOC;
14984  LRETURN;
14985  }
14986 
14987  memcpy(p_nb_data, nb_data, nb_size);
14988 
14989  /* configure network binary size to be written */
14991  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, nb_size %u\n", __func__, ui32LBA,
14992  nb_size);
14993 
14994  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
14995  p_ai_config, config_size, ui32LBA);
14996  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
14997  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
14998  if (NI_RETCODE_SUCCESS != retval)
14999  {
15000  ni_log2(p_ctx, NI_LOG_ERROR,
15001  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
15002  ", hw_id, %u, xcoder_inst_id: %d\n",
15003  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15004  //Close the session since we can't configure it
15005  retval = ni_ai_session_close(p_ctx, 0);
15006  if (NI_RETCODE_SUCCESS != retval)
15007  {
15008  ni_log2(p_ctx, NI_LOG_ERROR,
15009  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
15010  ", hw_id, %u, xcoder_inst_id: %d\n",
15011  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
15012  p_ctx->session_id);
15013  }
15014 
15016  LRETURN;
15017  }
15018 
15019  dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15020  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15021  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
15022  {
15023  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
15024  retval = NI_RETCODE_ERROR_MEM_ALOC;
15025  LRETURN;
15026  }
15027 
15028  for (;;)
15029  {
15030  /* test if the model is already exist. if not, then continue to write binary data */
15031  memset(p_buffer, 0, dataLen);
15033  "6J") >= 0)
15034  {
15035  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
15037  } else
15038  {
15039  ui32LBA =
15041  }
15042  retval =
15044  p_buffer, dataLen, ui32LBA);
15045  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15046  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15047  if (retval != NI_RETCODE_SUCCESS)
15048  {
15049  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
15051  LRETURN;
15052  }
15053  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
15054  {
15055  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): network binary registered\n", __func__);
15056  LRETURN;
15057  }
15058 
15059  /* if this model is not ready, try to require nbuffer to write */
15061  NI_DEVICE_TYPE_AI, &buf_info);
15062  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15063  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15064  if (retval != NI_RETCODE_SUCCESS)
15065  {
15066  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Query Write buffer Failed\n", __func__);
15067  LRETURN;
15068  }
15069  if (buf_info.buf_avail_size < nb_size)
15070  {
15071  /*
15072  * Cannot aquire the write access to nbuffer because someone else on the go,
15073  * Retry to see if model is ready.
15074  */
15075  ni_log2(p_ctx, NI_LOG_TRACE,
15076  "AI write query failed or buf_size %u < "
15077  "frame_size %u.\n", buf_info.buf_avail_size, nb_size);
15078  } else
15079  {
15080  /* Grant write access to nbuffer */
15081  ni_log2(p_ctx, NI_LOG_DEBUG,
15082  "Info ai write query success, available buf "
15083  "size %u >= frame size %u !\n",
15084  buf_info.buf_avail_size, nb_size);
15085  break;
15086  }
15087  retry_count++;
15088  /* Wait for at least 30s */
15090  {
15091  ni_pthread_mutex_unlock(&p_ctx->mutex);
15092  ni_usleep(100);
15093  ni_pthread_mutex_lock(&p_ctx->mutex);
15094  } else
15095  {
15096  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
15098  LRETURN;
15099  }
15100  }
15101 
15102 #if 1
15103  /* write network binary data */
15104  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
15105  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb LBA 0x%x\n", __func__, ui32LBA);
15106  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15107  p_nb_data, buffer_size, ui32LBA);
15108  ni_log2(p_ctx, NI_LOG_DEBUG, "write complete retval %d\n", retval);
15109  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
15110  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15111  if (NI_RETCODE_SUCCESS != retval)
15112  {
15113  ni_log2(p_ctx, NI_LOG_ERROR,
15114  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
15115  ", hw_id, %u, xcoder_inst_id: %d\n",
15116  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15117  //Close the session since we can't configure it
15118  retval = ni_ai_session_close(p_ctx, 0);
15119  if (NI_RETCODE_SUCCESS != retval)
15120  {
15121  ni_log2(p_ctx, NI_LOG_ERROR,
15122  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
15123  ", hw_id, %u, xcoder_inst_id: %d\n",
15124  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
15125  p_ctx->session_id);
15126  }
15127 
15129  LRETURN;
15130  }
15131 
15132 #else
15133  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write nb buffer_size %u\n", __func__,
15134  buffer_size);
15135  for (transferred = 0; transferred < buffer_size; transferred += this_size)
15136  {
15137  this_size = p_ctx->max_nvme_io_size < (buffer_size - transferred) ?
15138  p_ctx->max_nvme_io_size :
15139  (buffer_size - transferred);
15140 
15141  if (this_size & (4096 - 1))
15142  {
15143  this_size = (this_size + (4096 - 1)) & ~(4096 - 1);
15144  }
15145  /* write network binary data */
15146  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI) +
15147  (transferred >> 12);
15148  ni_log2(p_ctx, NI_LOG_DEBUG,
15149  "%s(): write nb LBA 0x%x, this_size %u, page_offset %u\n",
15150  __func__, ui32LBA, this_size, (transferred >> 12));
15151  p_data = (uint8_t *)p_nb_data + transferred;
15152  retval = ni_nvme_send_write_cmd(
15153  p_ctx->blk_io_handle, p_ctx->event_handle,
15154  (uint8_t *)p_nb_data + transferred, this_size, ui32LBA);
15155  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): write retval %d\n", __func__, retval);
15156  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
15157  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15158  if (NI_RETCODE_SUCCESS != retval)
15159  {
15160  ni_log(
15161  NI_LOG_ERROR,
15162  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
15163  ", hw_id, %u, xcoder_inst_id: %d\n",
15164  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
15165  //Close the session since we can't configure it
15166  retval = ni_ai_session_close(p_ctx, 0);
15167  if (NI_RETCODE_SUCCESS != retval)
15168  {
15169  ni_log(
15170  NI_LOG_ERROR,
15171  "ERROR: ni_ai_session_close failed: blk_io_handle: %" PRIx64
15172  ", hw_id, %u, xcoder_inst_id: %d\n",
15173  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
15174  p_ctx->session_id);
15175  }
15176 
15178  LRETURN;
15179  }
15180  }
15181 
15182 #endif
15183 
15184 END:
15185  ni_pthread_mutex_unlock(&p_ctx->mutex);
15186 
15187  ni_aligned_free(p_ai_config);
15188  ni_aligned_free(p_nb_data);
15189  ni_aligned_free(p_buffer);
15190 
15191  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15192 
15193  return retval;
15194 }
15195 
15197 {
15199  void *p_buffer = NULL;
15200  uint32_t ui32LBA = 0;
15201  int retry_count = 0;
15202  uint32_t dataLen = (sizeof(ni_network_layer_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15203  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15204  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
15205  {
15206  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: Cannot allocate buffer.\n");
15207  retval = NI_RETCODE_ERROR_MEM_ALOC;
15208  LRETURN;
15209  }
15210  for(;;)
15211  {
15212  memset(p_buffer, 0, dataLen);
15213  ui32LBA =
15215  retval =
15217  p_buffer, dataLen, ui32LBA);
15218  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15219  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15220  if (retval != NI_RETCODE_SUCCESS)
15221  {
15222  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
15223  }
15224  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
15225  {
15226  ni_log2(p_ctx, NI_LOG_INFO, "%s(): network binary registered\n", __func__);
15227  break;
15228  }
15229 
15230  retry_count++;
15231  /* Wait for at least 30s */
15233  {
15234  ni_usleep(100);
15235  } else
15236  {
15237  ni_log2(p_ctx, NI_LOG_DEBUG, "AI network binary configuration polling timeout\n");
15239  LRETURN;
15240  }
15241  }
15242 END:
15243 
15244  ni_aligned_free(p_buffer);
15245  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15246 
15247  return retval;
15248 
15249 }
15250 
15252  ni_frame_t *p_frame)
15253 {
15255  uint32_t ui32LBA = 0;
15256  ni_instance_buf_info_t buf_info = {0};
15257  uint32_t frame_size_bytes;
15258  uint32_t sent_size = 0;
15259  int32_t query_retry = 0;
15260 
15261  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15262 
15263  if (!p_ctx || !p_frame)
15264  {
15265  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): passed parameters is null\n",
15266  __func__);
15267  retval = NI_RETCODE_INVALID_PARAM;
15268  return retval;
15269  }
15270 
15271  if (p_frame->data_len[0] == 0)
15272  {
15273  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() invalid data length\n",
15274  __func__);
15275  retval = NI_RETCODE_INVALID_PARAM;
15276  return retval;
15277  }
15278 
15279  ni_pthread_mutex_lock(&p_ctx->mutex);
15280 
15281 
15282  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15283  {
15284  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15285  __func__);
15287  LRETURN;
15288  }
15289 
15290  frame_size_bytes = p_frame->data_len[0];
15291  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): frame_size_bytes %u\n", __func__,
15292  frame_size_bytes);
15293 
15294  for (;;)
15295  {
15296  if (p_ctx->session_statistic.ui32WrBufAvailSize >= frame_size_bytes)
15297  {
15298  buf_info.buf_avail_size =
15300  ni_log2(p_ctx, NI_LOG_DEBUG,
15301  "Info ai write query success, available buf "
15302  "size %u >= frame size %u !\n",
15303  buf_info.buf_avail_size, frame_size_bytes);
15304  break;
15305  }
15306 
15308  "6K") >= 0)
15309  {
15311  &p_ctx->session_statistic);
15312  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
15314  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
15315  buf_info.buf_avail_size =
15317  } else
15318  {
15320  NI_DEVICE_TYPE_AI, &buf_info);
15321  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15322  p_ctx->device_type, p_ctx->hw_id,
15323  &(p_ctx->session_id), OPT_1);
15324  }
15325  if (NI_RETCODE_SUCCESS != retval ||
15326  buf_info.buf_avail_size < frame_size_bytes)
15327  {
15328  ni_log2(p_ctx, NI_LOG_TRACE,
15329  "AI write query failed or buf_size < frame_size. Retry %d\n",
15330  query_retry);
15331  // extend to 5000 retries for 8K encode on FPGA
15332  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
15333  {
15334  ni_log2(p_ctx, NI_LOG_DEBUG,
15335  "AI write query exceeded max retries: %d\n",
15338  retval = NI_RETCODE_SUCCESS;
15339  LRETURN;
15340  }
15341  ni_pthread_mutex_unlock(&p_ctx->mutex);
15343  ni_pthread_mutex_lock(&p_ctx->mutex);
15344  query_retry++;
15345  }
15346  }
15347 
15348  ui32LBA = WRITE_INSTANCE_W(p_ctx->session_id, NI_DEVICE_TYPE_AI);
15349  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai session write: p_data = %p, p_frame->buffer_size = %u, "
15350  "p_ctx->frame_num = %" PRIu64 ", LBA = 0x%x\n",
15351  p_frame->p_data, p_frame->buffer_size, p_ctx->frame_num,
15352  ui32LBA);
15353 
15354  sent_size = frame_size_bytes;
15355  if (sent_size & (NI_MEM_PAGE_ALIGNMENT - 1))
15356  {
15357  sent_size = (sent_size + NI_MEM_PAGE_ALIGNMENT - 1) &
15358  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15359  }
15360 
15361  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15362  p_frame->p_buffer, sent_size, ui32LBA);
15364  "6K") >= 0)
15365  {
15366  if (retval != NI_RETCODE_SUCCESS)
15367  {
15368  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15370  LRETURN;
15371  }
15373  &p_ctx->session_statistic);
15374  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
15376  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
15377  } else
15378  {
15379  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_write, p_ctx->device_type,
15380  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15381  if (retval < 0)
15382  {
15383  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15385  LRETURN;
15386  }
15387  }
15388 
15389  p_ctx->frame_num++;
15390 
15391  retval = frame_size_bytes;
15392 
15393 END:
15394 
15395  ni_pthread_mutex_unlock(&p_ctx->mutex);
15396 
15397  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15398 
15399  return retval;
15400 }
15401 
15403  ni_packet_t *p_packet)
15404 {
15405  uint32_t actual_read_size = 0;
15406  int retval = NI_RETCODE_SUCCESS;
15407  uint32_t ui32LBA = 0;
15408  ni_instance_buf_info_t buf_info = {0};
15409 
15410  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15411 
15412  if (!p_ctx || !p_packet || !p_packet->p_data)
15413  {
15414  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15415  __func__);
15416  retval = NI_RETCODE_INVALID_PARAM;
15417  return retval;
15418  }
15419 
15420  ni_pthread_mutex_lock(&p_ctx->mutex);
15421 
15422  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15423  {
15424  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15425  __func__);
15427  LRETURN;
15428  }
15429 
15430  for (;;)
15431  {
15432  if (p_ctx->session_statistic.ui32RdBufAvailSize >= p_packet->data_len)
15433  {
15434  buf_info.buf_avail_size =
15436  ni_log2(p_ctx, NI_LOG_DEBUG,
15437  "Info ai read query success, available buf "
15438  "size %u >= frame size %u !\n",
15439  buf_info.buf_avail_size, p_packet->data_len);
15440  break;
15441  }
15443  "6K") >= 0)
15444  {
15446  &p_ctx->session_statistic);
15447  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
15449  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
15450  buf_info.buf_avail_size =
15452  } else
15453  {
15455  NI_DEVICE_TYPE_AI, &buf_info);
15456  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
15457  p_ctx->device_type, p_ctx->hw_id,
15458  &(p_ctx->session_id), OPT_1);
15459  }
15460  ni_log2(p_ctx, NI_LOG_TRACE,
15461  "Info ai read query rc %d, available buf size %u, "
15462  "frame_num=%" PRIu64 ", pkt_num=%" PRIu64 "\n",
15463  retval, buf_info.buf_avail_size, p_ctx->frame_num,
15464  p_ctx->pkt_num);
15465 
15466  if (NI_RETCODE_SUCCESS != retval)
15467  {
15468  ni_log2(p_ctx, NI_LOG_DEBUG, "Buffer info query failed in ai read!!!!\n");
15469  LRETURN;
15470  } else if (0 == buf_info.buf_avail_size)
15471  {
15472  ni_log2(p_ctx, NI_LOG_DEBUG, "Info ai read available buf size %u, eos %u !\n",
15473  buf_info.buf_avail_size, p_packet->end_of_stream);
15474  retval = NI_RETCODE_SUCCESS;
15475  LRETURN;
15476  }
15477  }
15478  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai read buf_avail_size %u\n", buf_info.buf_avail_size);
15479 
15480  assert(buf_info.buf_avail_size >= p_packet->data_len);
15481 
15482  ui32LBA = READ_INSTANCE_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
15483  actual_read_size = p_packet->data_len;
15484  if (actual_read_size & (NI_MEM_PAGE_ALIGNMENT - 1))
15485  {
15486  actual_read_size = (actual_read_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15487  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15488  }
15489 
15490  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15491  p_packet->p_data, actual_read_size, ui32LBA);
15493  "6K") >= 0)
15494  {
15495  if (retval != NI_RETCODE_SUCCESS)
15496  {
15497  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15499  LRETURN;
15500  }
15502  &p_ctx->session_statistic);
15503  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
15505  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
15506  } else
15507  {
15508  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
15509  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15510  if (retval < 0)
15511  {
15512  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed\n", __func__);
15514  LRETURN;
15515  }
15516  }
15517 
15518  retval = p_packet->data_len;
15519 
15520 END:
15521 
15522  ni_pthread_mutex_unlock(&p_ctx->mutex);
15523 
15524  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15525 
15526  return retval;
15527 }
15528 
15529 static void ni_unreference_network_data(ni_network_data_t *network_data)
15530 {
15531  if (network_data)
15532  {
15533  ni_memfree(network_data->inset);
15534  ni_memfree(network_data->linfo.in_param);
15535  }
15536 }
15537 
15539  ni_network_data_t *p_network)
15540 {
15541  void *p_buffer = NULL;
15542  void *p_info = NULL;
15544  uint32_t ui32LBA = 0;
15545  uint32_t dataLen;
15546  int32_t query_retry = 0;
15547  int l;
15548  ni_network_layer_params_t *layer_param;
15549  uint32_t buffer_size;
15550  uint32_t this_size;
15551  ni_network_data_t *network_data = NULL;
15552  uint32_t total_io_num;
15553 
15554  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15555 
15556  if (!p_ctx || !p_network)
15557  {
15558  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15559  __func__);
15560  retval = NI_RETCODE_INVALID_PARAM;
15561  LRETURN;
15562  }
15563 
15564  network_data = p_ctx->network_data;
15565  if (network_data->linfo.in_param != NULL)
15566  {
15567  ni_log2(p_ctx, NI_LOG_ERROR,
15568  "ERROR: %s(): network parameters data is already initialized\n",
15569  __func__);
15570  retval = NI_RETCODE_SUCCESS;
15571  LRETURN;
15572  }
15573 
15574  ni_pthread_mutex_lock(&p_ctx->mutex);
15575 
15576  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15577  {
15578  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
15579  __func__);
15581  LRETURN;
15582  }
15583 
15584  /* query available size can be read. */
15585  dataLen = (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15586  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15587  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
15588  {
15589  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
15590  NI_ERRNO, __func__);
15591  retval = NI_RETCODE_ERROR_MEM_ALOC;
15592  LRETURN;
15593  }
15594  memset(p_buffer, 0, dataLen);
15595 
15596  for (;;)
15597  {
15599  "6J") >= 0)
15600  {
15601  ui32LBA = QUERY_INSTANCE_NL_SIZE_V2_R(p_ctx->session_id,
15603  } else
15604  {
15605  ui32LBA =
15607  }
15608  retval =
15610  p_buffer, dataLen, ui32LBA);
15611  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
15612  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15613  if (retval != NI_RETCODE_SUCCESS)
15614  {
15615  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
15617  LRETURN;
15618  }
15619 
15620  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size > 0)
15621  {
15622  break;
15623  }
15624 
15625  query_retry++;
15626  if (query_retry > 50000)
15627  {
15628  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
15629  __func__, query_retry - 1);
15631  LRETURN;
15632  }
15633  ni_pthread_mutex_unlock(&p_ctx->mutex);
15634  ni_usleep(200);
15635  ni_pthread_mutex_lock(&p_ctx->mutex);
15636  continue;
15637  }
15638 
15639  if (((ni_instance_buf_info_t *)p_buffer)->buf_avail_size == 0)
15640  {
15642  LRETURN;
15643  }
15644 
15646  "6J") >= 0)
15647  {
15648  network_data->input_num =
15649  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size >> 16;
15650  network_data->output_num =
15651  ((ni_instance_buf_info_t *)p_buffer)->buf_avail_size & 0xFFFF;
15652  } else
15653  {
15654  network_data->input_num = 4;
15655  network_data->output_num = 4;
15656  }
15657 
15658  total_io_num = network_data->input_num + network_data->output_num;
15659 
15660  network_data->linfo.in_param = (ni_network_layer_params_t *)calloc(
15661  total_io_num, sizeof(ni_network_layer_params_t));
15662  if (!network_data->linfo.in_param)
15663  {
15664  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network layer params\n",
15665  __func__);
15666  retval = NI_RETCODE_ERROR_MEM_ALOC;
15667  LRETURN;
15668  }
15669  network_data->linfo.out_param =
15670  network_data->linfo.in_param + network_data->input_num;
15671 
15672  network_data->inset = (ni_network_layer_offset_t *)calloc(
15673  total_io_num, sizeof(ni_network_layer_offset_t));
15674  if (!network_data->inset)
15675  {
15676  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): Unable to allocate network offset\n",
15677  __func__);
15678  retval = NI_RETCODE_ERROR_MEM_ALOC;
15679  LRETURN;
15680  }
15681  network_data->outset = network_data->inset + network_data->input_num;
15682 
15684  "6J") >= 0)
15685  {
15686  /* query the real network layer data */
15687  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
15688  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15689  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15690  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
15691  {
15692  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
15693  NI_ERRNO, __func__);
15694  retval = NI_RETCODE_ERROR_MEM_ALOC;
15695  LRETURN;
15696  }
15697  memset(p_info, 0, dataLen);
15698 
15700  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15701  p_info, dataLen, ui32LBA);
15702  if ((int32_t)retval < 0)
15703  {
15704  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
15706  LRETURN;
15707  }
15708  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
15709  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15710  memcpy(network_data->linfo.in_param, p_info, this_size);
15711  } else
15712  {
15713  /* query the real network layer data */
15714  this_size = sizeof(ni_network_layer_params_t) * total_io_num;
15715  dataLen = (this_size + (NI_MEM_PAGE_ALIGNMENT - 1)) &
15716  ~(NI_MEM_PAGE_ALIGNMENT - 1);
15717  if (ni_posix_memalign(&p_info, sysconf(_SC_PAGESIZE), dataLen))
15718  {
15719  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate info buffer\n",
15720  NI_ERRNO, __func__);
15721  retval = NI_RETCODE_ERROR_MEM_ALOC;
15722  LRETURN;
15723  }
15724  memset(p_info, 0, dataLen);
15725 
15727  retval = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
15728  p_info, dataLen, ui32LBA);
15729  if ((int32_t)retval < 0)
15730  {
15731  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
15733  LRETURN;
15734  }
15735  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
15736  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15737  memcpy(network_data->linfo.in_param, p_info, this_size);
15738 
15739  for (l = 0, network_data->input_num = 0; l < 4; l++)
15740  {
15741  layer_param = &network_data->linfo.in_param[l];
15742  if (layer_param->num_of_dims == 0)
15743  {
15744  break;
15745  }
15746  network_data->input_num++;
15747  }
15748 
15749  for (l = 0, network_data->output_num = 0; l < 4; l++)
15750  {
15751  layer_param = &network_data->linfo.out_param[l];
15752  if (layer_param->num_of_dims == 0)
15753  {
15754  break;
15755  }
15756  network_data->output_num++;
15757  }
15758  }
15759 
15760  for (l = 0, buffer_size = 0; l < network_data->input_num; l++)
15761  {
15762  layer_param = &network_data->linfo.in_param[l];
15763  this_size = ni_ai_network_layer_size(layer_param);
15764  this_size =
15765  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
15766  network_data->inset[l].offset = buffer_size;
15767  buffer_size += this_size;
15768 
15769  ni_log2(p_ctx,
15770  NI_LOG_DEBUG,
15771  "%s(): network input layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
15772  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
15773  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
15774  layer_param->data_format, layer_param->quant_format);
15775  }
15776 
15777  for (l = 0, buffer_size = 0; l < network_data->output_num; l++)
15778  {
15779  layer_param = &network_data->linfo.out_param[l];
15780  this_size = ni_ai_network_layer_size(layer_param);
15781  this_size =
15782  (this_size + NI_AI_HW_ALIGN_SIZE - 1) & ~(NI_AI_HW_ALIGN_SIZE - 1);
15783  network_data->outset[l].offset = buffer_size;
15784  buffer_size += this_size;
15785 
15786  ni_log2(p_ctx,
15787  NI_LOG_DEBUG,
15788  "%s(): network output layer %d: dims %u, %u/%u/%u/%u, f %d, q %d\n",
15789  __func__, l, layer_param->num_of_dims, layer_param->sizes[0],
15790  layer_param->sizes[1], layer_param->sizes[2], layer_param->sizes[3],
15791  layer_param->data_format, layer_param->quant_format);
15792  }
15793 
15794  memcpy(p_network, network_data, sizeof(ni_network_data_t));
15795 
15796  retval = NI_RETCODE_SUCCESS;
15797 
15798 END:
15799  ni_pthread_mutex_unlock(&p_ctx->mutex);
15800 
15801  if (retval != NI_RETCODE_SUCCESS)
15802  {
15803  ni_unreference_network_data(network_data);
15804  }
15805  ni_aligned_free(p_buffer);
15806  ni_aligned_free(p_info);
15807  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15808  return retval;
15809 }
15810 
15812 {
15814  void *p_buffer = NULL;
15815  uint32_t ui32LBA = 0;
15816 
15817  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15818 
15819  if (!p_ctx)
15820  {
15821  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15822  __func__);
15823  retval = NI_RETCODE_INVALID_PARAM;
15824  LRETURN;
15825  }
15826 
15827  //Check if there is an instance or we need a new one
15828  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15829  {
15830  p_ctx->device_type = NI_DEVICE_TYPE_AI;
15831  p_ctx->pts_table = NULL;
15832  p_ctx->dts_queue = NULL;
15833  p_ctx->buffer_pool = NULL;
15834  p_ctx->status = 0;
15835  p_ctx->key_frame_type = 0;
15836  p_ctx->keyframe_factor = 1;
15837  p_ctx->frame_num = 0;
15838  p_ctx->pkt_num = 0;
15839  p_ctx->rc_error_count = 0;
15840  p_ctx->force_frame_type = 0;
15841  p_ctx->ready_to_close = 0;
15842  //Sequence change tracking reated stuff
15843  p_ctx->active_video_width = 0;
15844  p_ctx->active_video_height = 0;
15845  p_ctx->p_all_zero_buf = NULL;
15846  p_ctx->actual_video_width = 0;
15848  memset(&(p_ctx->param_err_msg[0]), 0, sizeof(p_ctx->param_err_msg));
15849 
15850  //malloc zero data buffer
15851  if (ni_posix_memalign(&p_ctx->p_all_zero_buf, sysconf(_SC_PAGESIZE),
15853  {
15854  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc all zero buffer failed\n",
15855  NI_ERRNO, __func__);
15856  retval = NI_RETCODE_ERROR_MEM_ALOC;
15857  LRETURN;
15858  }
15859  memset(p_ctx->p_all_zero_buf, 0, NI_DATA_BUFFER_LEN);
15860 
15861  //malloc data buffer
15862  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
15864  {
15865  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
15866  NI_ERRNO, __func__);
15867  retval = NI_RETCODE_ERROR_MEM_ALOC;
15868  LRETURN;
15869  }
15870  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
15871 
15872  //Set session ID to be invalid. In case we cannot open session, the session id wold remain invalid.
15873  //In case we can open sesison, the session id would become valid.
15874  ((ni_session_stats_t *)p_buffer)->ui16SessionId =
15875  (uint16_t)NI_INVALID_SESSION_ID;
15876 
15877  // First uint32_t is either an invaild session ID or a valid session ID, depending on if session could be opened
15878  ui32LBA = OPEN_SESSION_CODEC(NI_DEVICE_TYPE_AI, 0, p_ctx->hw_action);
15879  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): LBA 0x%x, hw_action %d\n", __func__,
15880  ui32LBA, p_ctx->hw_action);
15881  retval =
15883  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
15884  if (retval != NI_RETCODE_SUCCESS)
15885  {
15886  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR ni_nvme_send_read_cmd\n");
15887  LRETURN;
15888  }
15889  //Open will return a session status structure with a valid session id if it worked.
15890  //Otherwise the invalid session id set before the open command will stay
15891  if ((uint16_t)NI_INVALID_SESSION_ID ==
15892  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId))
15893  {
15894  ni_log2(p_ctx, NI_LOG_ERROR,
15895  "ERROR %s(): p_ctx->device_handle=%" PRIx64
15896  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
15897  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
15898  p_ctx->session_id);
15899  ni_ai_session_close(p_ctx, 0);
15901  LRETURN;
15902  }
15903  p_ctx->session_id =
15904  ni_ntohs(((ni_session_stats_t *)p_buffer)->ui16SessionId);
15905  p_ctx->session_timestamp = ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_high);
15906  p_ctx->session_timestamp = (p_ctx->session_timestamp << 32) |
15907  ni_htonl(((ni_session_stats_t *)p_buffer)->ui32Session_timestamp_low);
15908  ni_log2(p_ctx, NI_LOG_DEBUG, "Ai open session ID:0x%x,timestamp:%" PRIu64 "\n",
15909  p_ctx->session_id, p_ctx->session_timestamp);
15910 
15911  ni_log2(p_ctx, NI_LOG_DEBUG, "Open session completed\n");
15912  ni_log2(p_ctx, NI_LOG_DEBUG,
15913  "%s(): p_ctx->device_handle=%" PRIx64
15914  ", p_ctx->hw_id=%d, p_ctx->session_id=%d\n",
15915  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
15916  p_ctx->session_id);
15917 
15918  //Send keep alive timeout Info
15919  uint64_t keep_alive_timeout =
15920  p_ctx->keep_alive_timeout * 1000000; //send us to FW
15921  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
15922  memcpy(p_buffer, &keep_alive_timeout, sizeof(keep_alive_timeout));
15923  ni_log2(p_ctx, NI_LOG_DEBUG, "%s keep_alive_timeout %" PRIx64 "\n", __func__,
15924  keep_alive_timeout);
15926  retval =
15928  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
15929  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
15930  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
15931  CHECK_VPU_RECOVERY(retval);
15932 
15933  if (NI_RETCODE_SUCCESS != retval)
15934  {
15935  ni_log2(p_ctx, NI_LOG_ERROR,
15936  "ERROR %s(): nvme write keep_alive_timeout command "
15937  "failed, blk_io_handle: %" PRIx64 ", hw_id, %d\n",
15938  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id);
15940  LRETURN;
15941  }
15942 
15943  p_ctx->network_data =
15944  (ni_network_data_t *)calloc(1, sizeof(ni_network_data_t));
15945  if (!p_ctx->network_data)
15946  {
15947  ni_log2(p_ctx, NI_LOG_ERROR,
15948  "ERROR %s(): Unable to allocate network_data memory\n");
15949  retval = NI_RETCODE_ERROR_MEM_ALOC;
15950  LRETURN;
15951  }
15952  }
15953 
15954  // init for frame pts calculation
15955  p_ctx->is_first_frame = 1;
15956  p_ctx->last_pts = 0;
15957  p_ctx->last_dts = 0;
15958  p_ctx->active_video_width = 0;
15959  p_ctx->active_video_height = 0;
15960  p_ctx->actual_video_width = 0;
15961 
15962 END:
15963 
15964  ni_aligned_free(p_buffer);
15965  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
15966  return retval;
15967 }
15968 
15970 {
15972  void *p_buffer = NULL;
15973  uint32_t ui32LBA = 0;
15974 
15975  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
15976 
15977  if (!p_ctx)
15978  {
15979  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
15980  __func__);
15981  return NI_RETCODE_INVALID_PARAM;
15982  }
15983 
15984  ni_pthread_mutex_lock(&p_ctx->mutex);
15985 
15986  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
15987  {
15988  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): Invalid session ID, return.\n", __func__);
15989  retval = NI_RETCODE_SUCCESS;
15990  LRETURN;
15991  }
15992 
15993  //malloc data buffer
15994  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN))
15995  {
15996  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() alloc data buffer failed\n",
15997  NI_ERRNO, __func__);
15998  retval = NI_RETCODE_ERROR_MEM_ALOC;
15999  LRETURN;
16000  }
16001  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
16002 
16003  ui32LBA = CLOSE_SESSION_R(p_ctx->session_id, NI_DEVICE_TYPE_AI);
16004 
16005  int retry = 0;
16006  while (retry < NI_SESSION_CLOSE_RETRY_MAX)
16007  {
16008  ni_log2(p_ctx, NI_LOG_DEBUG,
16009  "%s(): p_ctx->blk_io_handle=%" PRIx64 ", p_ctx->hw_id=%d, "
16010  "p_ctx->session_id=%d, close_mode=1\n",
16011  __func__, (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id,
16012  p_ctx->session_id);
16013 
16015  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA) < 0)
16016  {
16017  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): command failed\n", __func__);
16020  break;
16021  } else
16022  {
16023  //Close should always succeed
16024  retval = NI_RETCODE_SUCCESS;
16026  break;
16027  }
16028  /*
16029  else if(((ni_session_closed_status_t *)p_buffer)->session_closed)
16030  {
16031  retval = NI_RETCODE_SUCCESS;
16032  p_ctx->session_id = NI_INVALID_SESSION_ID;
16033  break;
16034  }
16035  else
16036  {
16037  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): wait for close\n", __func__);
16038  ni_usleep(NI_SESSION_CLOSE_RETRY_INTERVAL_US);
16039  retval = NI_RETCODE_ERROR_NVME_CMD_FAILED;
16040  }
16041  */
16042  retry++;
16043  }
16044 
16045 END:
16046 
16047  ni_unreference_network_data(p_ctx->network_data);
16048  ni_memfree(p_ctx->network_data);
16049 
16050  ni_aligned_free(p_buffer);
16052 
16053  //Sequence change related stuff cleanup here
16054  p_ctx->active_video_width = 0;
16055  p_ctx->active_video_height = 0;
16056  p_ctx->actual_video_width = 0;
16057 
16058  //End of sequence change related stuff cleanup
16060  p_ctx->buffer_pool = NULL;
16061 
16062  ni_log2(p_ctx, NI_LOG_DEBUG, "%s(): CTX[Card:%" PRIx64 " / HW:%d / INST:%d]\n",
16063  __func__, (int64_t)p_ctx->device_handle, p_ctx->hw_id,
16064  p_ctx->session_id);
16065  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16066 
16067  ni_pthread_mutex_unlock(&p_ctx->mutex);
16068 
16069  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16070 
16071  return retval;
16072 }
16073 
16075  ni_frame_config_t p_cfg_in[],
16076  int numInCfgs,
16077  ni_frame_config_t *p_cfg_out)
16078 {
16080  ni_network_buffer_t *p_data = NULL;
16081  void *p_read_data = NULL;
16082  uint32_t dataLen;
16083  uint32_t ui32LBA = 0;
16084 
16085  if (!p_ctx)
16086  {
16087  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16088  __func__);
16089  return NI_RETCODE_INVALID_PARAM;
16090  }
16091 
16092  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
16093  {
16094  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16095  __func__);
16097  }
16098  ni_pthread_mutex_lock(&p_ctx->mutex);
16099  ni_instance_buf_info_t buf_info = {0};
16100  int32_t query_retry = 0;
16101  for (;;)
16102  {
16103  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
16104  {
16105  buf_info.buf_avail_size =
16107  ni_log2(p_ctx, NI_LOG_DEBUG,
16108  "Info ai write query success, available buf "
16109  "size %u !\n",
16110  buf_info.buf_avail_size);
16111  break;
16112  }
16113 
16115  &p_ctx->session_statistic);
16116  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16118  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16119  buf_info.buf_avail_size =
16121  if (NI_RETCODE_SUCCESS != retval ||
16122  buf_info.buf_avail_size == 0)
16123  {
16124  ni_log2(p_ctx, NI_LOG_TRACE,
16125  "AI write query failed or buf_size < frame_size. Retry %d\n",
16126  query_retry);
16127  // extend to 5000 retries for 8K encode on FPGA
16128  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
16129  {
16130  ni_log2(p_ctx, NI_LOG_TRACE,
16131  "AI write query exceeded max retries: %d\n",
16134  retval = NI_RETCODE_SUCCESS;
16135  LRETURN;
16136  }
16137  ni_pthread_mutex_unlock(&p_ctx->mutex);
16139  ni_pthread_mutex_lock(&p_ctx->mutex);
16140  query_retry++;
16141  }
16142  }
16143 
16144  dataLen = (sizeof(ni_network_buffer_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
16145  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16146 
16147  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
16148  {
16149  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16150  NI_ERRNO, __func__);
16152  }
16153 
16154  memset(p_data, 0x00, dataLen);
16155  p_data->ui16Width = p_cfg_in[0].picture_width;
16156  p_data->ui16Height = p_cfg_in[0].picture_height;
16157  p_data->ui16Option = p_cfg_in[0].options;
16158  p_data->ui8PoolSize = p_cfg_in[0].rgba_color;
16159  p_data->ui8MultiIn = 1;
16160  for(int i = 0; i < numInCfgs; i++){
16161  p_data->ui16FrameIdx[i] = p_cfg_in[i].frame_index;
16162  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame[%d]: frame_index %u, hw=%d\n",
16163  i,p_data->ui16FrameIdx[i], p_data->ui8MultiIn);
16164  }
16165 
16166 
16167  ui32LBA =
16169 
16170  retval =
16172  p_data, dataLen, ui32LBA);
16173  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16174  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
16175  OPT_1);
16176  if (NI_RETCODE_SUCCESS != retval)
16177  {
16178  ni_log2(p_ctx,
16179  NI_LOG_ERROR,
16180  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16181  ", hw_id, %u, xcoder_inst_id: %d\n",
16182  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16183  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
16184  __func__);
16186  LRETURN;
16187  }
16188 
16189  END:
16190  ni_pthread_mutex_unlock(&p_ctx->mutex);
16191  ni_aligned_free(p_data);
16192  if (p_read_data != NULL)
16193  {
16194  ni_aligned_free(p_read_data);
16195  }
16196  return retval;
16197 }
16198 
16200  niFrameSurface1_t *p_out_surface)
16201 {
16203  ni_network_buffer_t *p_data = NULL;
16204  void *p_read_data = NULL;
16205  uint32_t dataLen;
16206  uint32_t ui32LBA = 0;
16207 
16208  if (!p_ctx)
16209  {
16210  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16211  __func__);
16212  return NI_RETCODE_INVALID_PARAM;
16213  }
16214 
16215  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
16216  {
16217  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16218  __func__);
16220  }
16221 
16222  if (ni_cmp_fw_api_ver(
16224  "6rL") < 0)
16225  {
16226  ni_log2(p_ctx, NI_LOG_ERROR,
16227  "Error: %s function not supported on device with FW API version < 6rL\n",
16228  __func__);
16230  }
16231 
16232  ni_pthread_mutex_lock(&p_ctx->mutex);
16233 
16234  int query_retry = 0;
16235  dataLen =
16236  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16237  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16238  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
16239  {
16240  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16241  NI_ERRNO, __func__);
16242  retval = NI_RETCODE_ERROR_MEM_ALOC;
16243  LRETURN;
16244  }
16245  memset(p_read_data, 0, dataLen);
16246 
16247  for (;;)
16248  {
16249  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
16251  retval =
16253  p_read_data, dataLen, ui32LBA);
16254  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
16255  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
16256  OPT_1);
16257  if (retval != NI_RETCODE_SUCCESS)
16258  {
16259  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16261  LRETURN;
16262  }
16263 
16264  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail > 0)
16265  {
16266  p_out_surface->ui16FrameIdx = ((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.frame_index;
16267  p_out_surface->ui16session_ID = p_ctx->session_id;
16268  p_out_surface->device_handle =
16269  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
16270  p_out_surface->bit_depth = p_ctx->bit_depth_factor;
16271  p_out_surface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
16272  p_out_surface->output_idx = 0;
16273  break;
16274  }
16275 
16276  query_retry++;
16277  if (query_retry > 2000)
16278  {
16279  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
16280  __func__, query_retry - 1);
16281  retval = NI_RETCODE_EAGAIN;
16282  LRETURN;
16283  }
16284  ni_pthread_mutex_unlock(&p_ctx->mutex);
16285  ni_usleep(200);
16286  ni_pthread_mutex_lock(&p_ctx->mutex);
16287  continue;
16288  }
16289 
16290  if (((ni_instance_buf_info_t *)p_read_data)->hw_inst_ind.buffer_avail == 0)
16291  {
16293  LRETURN;
16294  }
16295 END:
16296  ni_pthread_mutex_unlock(&p_ctx->mutex);
16297  ni_aligned_free(p_data);
16298  if (p_read_data != NULL)
16299  {
16300  ni_aligned_free(p_read_data);
16301  }
16302  return retval;
16303 }
16304 
16306  int height, int options, int pool_size,
16307  int frame_index)
16308 {
16310  ni_network_buffer_t *p_data = NULL;
16311  void *p_read_data = NULL;
16312  uint32_t dataLen;
16313  uint32_t ui32LBA = 0;
16314 
16315  if (!p_ctx)
16316  {
16317  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s() passed parameters are null!, return\n",
16318  __func__);
16319  return NI_RETCODE_INVALID_PARAM;
16320  }
16321 
16322  if (p_ctx->session_id == NI_INVALID_SESSION_ID)
16323  {
16324  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16325  __func__);
16327  }
16328 
16329  ni_pthread_mutex_lock(&p_ctx->mutex);
16330 
16331  if (options == NI_AI_FLAG_IO)
16332  {
16333  int query_retry = 0;
16334  dataLen =
16335  (sizeof(ni_instance_buf_info_t) + (NI_MEM_PAGE_ALIGNMENT - 1)) &
16336  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16337  if (ni_posix_memalign(&p_read_data, sysconf(_SC_PAGESIZE), dataLen))
16338  {
16339  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16340  NI_ERRNO, __func__);
16341  retval = NI_RETCODE_ERROR_MEM_ALOC;
16342  LRETURN;
16343  }
16344  memset(p_read_data, 0, dataLen);
16345 
16346  for (;;)
16347  {
16348  ui32LBA = QUERY_INSTANCE_HW_OUT_SIZE_R(p_ctx->session_id,
16350  retval =
16352  p_read_data, dataLen, ui32LBA);
16353  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read,
16354  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
16355  OPT_1);
16356  if (retval != NI_RETCODE_SUCCESS)
16357  {
16358  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16360  LRETURN;
16361  }
16362 
16363  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size > 0)
16364  {
16365  break;
16366  }
16367 
16368  query_retry++;
16369  if (query_retry > 2000)
16370  {
16371  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): exceeded max query retries: %d\n",
16372  __func__, query_retry - 1);
16373  retval = NI_RETCODE_EAGAIN;
16374  LRETURN;
16375  }
16376  ni_pthread_mutex_unlock(&p_ctx->mutex);
16377  ni_usleep(200);
16378  ni_pthread_mutex_lock(&p_ctx->mutex);
16379  continue;
16380  }
16381 
16382  if (((ni_instance_buf_info_t *)p_read_data)->buf_avail_size == 0)
16383  {
16385  LRETURN;
16386  }
16387  } else
16388  {
16389  ni_instance_buf_info_t buf_info = {0};
16390  int32_t query_retry = 0;
16391  for (;;)
16392  {
16393  if (p_ctx->session_statistic.ui32WrBufAvailSize > 0)
16394  {
16395  buf_info.buf_avail_size =
16397  ni_log2(p_ctx, NI_LOG_DEBUG,
16398  "Info ai write query success, available buf "
16399  "size %u !\n",
16400  buf_info.buf_avail_size);
16401  break;
16402  }
16403 
16405  &p_ctx->session_statistic);
16406  CHECK_ERR_RC(p_ctx, retval, &p_ctx->session_statistic,
16408  p_ctx->hw_id, &(p_ctx->session_id), OPT_2);
16409  buf_info.buf_avail_size =
16411  if (NI_RETCODE_SUCCESS != retval ||
16412  buf_info.buf_avail_size == 0)
16413  {
16414  ni_log2(p_ctx, NI_LOG_TRACE,
16415  "AI write query failed or buf_size < frame_size. Retry %d\n",
16416  query_retry);
16417  // extend to 5000 retries for 8K encode on FPGA
16418  if (query_retry >= NI_MAX_ENCODER_QUERY_RETRIES)
16419  {
16420  ni_log2(p_ctx, NI_LOG_TRACE,
16421  "AI write query exceeded max retries: %d\n",
16424  retval = NI_RETCODE_SUCCESS;
16425  LRETURN;
16426  }
16427  ni_pthread_mutex_unlock(&p_ctx->mutex);
16429  ni_pthread_mutex_lock(&p_ctx->mutex);
16430  query_retry++;
16431  }
16432  }
16433 
16434  dataLen = (sizeof(ni_network_buffer_t) + NI_MEM_PAGE_ALIGNMENT - 1) &
16435  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16436 
16437  if (ni_posix_memalign((void **)&p_data, sysconf(_SC_PAGESIZE), dataLen))
16438  {
16439  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16440  NI_ERRNO, __func__);
16442  }
16443 
16444  memset(p_data, 0x00, dataLen);
16445  p_data->ui16FrameIdx[0] = frame_index;
16446  p_data->ui16Width = width;
16447  p_data->ui16Height = height;
16448  p_data->ui16Option = options;
16449  p_data->ui8PoolSize = pool_size;
16450  p_data->ui8MultiIn = 0;
16451  ni_log2(p_ctx, NI_LOG_DEBUG, "Dev alloc frame: frame_index %u\n",
16452  p_data->ui16FrameIdx[0]);
16453 
16454  ui32LBA =
16456 
16457  retval =
16459  p_data, dataLen, ui32LBA);
16460  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_config,
16461  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
16462  OPT_1);
16463  if (NI_RETCODE_SUCCESS != retval)
16464  {
16465  ni_log2(p_ctx,
16466  NI_LOG_ERROR,
16467  "ERROR: ni_nvme_send_admin_cmd failed: blk_io_handle: %" PRIx64
16468  ", hw_id, %u, xcoder_inst_id: %d\n",
16469  (int64_t)p_ctx->blk_io_handle, p_ctx->hw_id, p_ctx->session_id);
16470  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): nvme command failed!\n",
16471  __func__);
16473  LRETURN;
16474  }
16475  }
16476 
16477 END:
16478  ni_pthread_mutex_unlock(&p_ctx->mutex);
16479  ni_aligned_free(p_data);
16480  if (p_read_data != NULL)
16481  {
16482  ni_aligned_free(p_read_data);
16483  }
16484  return retval;
16485 }
16486 
16487 /*!******************************************************************************
16488  * \brief read a hardware descriptor from a scaler session
16489  *
16490  * \param[in] p_ctx pointer to session context
16491  * \param[out] p_frame pointer to frame to write hw descriptor
16492  *
16493  * \return NI_RETCODE_INVALID_PARAM
16494  * NI_RETCODE_ERROR_INVALID_SESSION
16495  * NI_RETCODE_ERROR_MEM_ALOC
16496  * NI_RETCODE_ERROR_NVME_CMD_FAILED
16497  * NI_RETCODE_FAILURE
16498  *******************************************************************************/
16500  ni_frame_t *p_frame)
16501 {
16502  int retval = NI_RETCODE_SUCCESS;
16503  int retry_count = 0;
16504 
16505  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16506 
16507  ni_pthread_mutex_lock(&p_ctx->mutex);
16508 
16509  if (NI_INVALID_SESSION_ID == p_ctx->session_id)
16510  {
16511  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %s(): Invalid session ID, return.\n",
16512  __func__);
16514  LRETURN;
16515  }
16516 
16517  for (;;)
16518  {
16519  retry_count++;
16520  ni_instance_buf_info_t sInstanceBuf = {0};
16521  niFrameSurface1_t *pFrameSurface;
16523  NI_DEVICE_TYPE_AI, &sInstanceBuf);
16524  CHECK_ERR_RC(p_ctx, retval, 0, nvme_admin_cmd_xcoder_query,
16525  p_ctx->device_type, p_ctx->hw_id, &(p_ctx->session_id),
16526  OPT_3);
16527 
16529  {
16530  if (retry_count >= 500)
16531  {
16532  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
16533  retval);
16534  LRETURN;
16535  }
16536  ni_pthread_mutex_unlock(&p_ctx->mutex);
16537  ni_usleep(100);
16538  ni_pthread_mutex_lock(&p_ctx->mutex);
16539  }
16540  else if (retval != NI_RETCODE_SUCCESS)
16541  {
16542  LRETURN;
16543  } else
16544  {
16545  pFrameSurface = (niFrameSurface1_t *)p_frame->p_data[3];
16546  pFrameSurface->ui16FrameIdx = sInstanceBuf.hw_inst_ind.frame_index;
16547  pFrameSurface->ui16session_ID = p_ctx->session_id;
16548  pFrameSurface->device_handle =
16549  (int32_t)((int64_t)p_ctx->blk_io_handle & 0xFFFFFFFF);
16550  pFrameSurface->src_cpu = (uint8_t)NI_DEVICE_TYPE_AI;
16551  pFrameSurface->output_idx = 0;
16552 
16553  /* A frame index of zero is invalid, the memory acquisition failed */
16554  if (pFrameSurface->ui16FrameIdx == 0)
16555  {
16556  if (retry_count >= 500)
16557  {
16558  ni_log2(p_ctx, NI_LOG_DEBUG, "Warning hwdesc read fail rc %d\n",
16559  retval);
16560  retval = NI_RETCODE_EAGAIN;
16561  LRETURN;
16562  }
16563  ni_pthread_mutex_unlock(&p_ctx->mutex);
16564  ni_usleep(100);
16565  ni_pthread_mutex_lock(&p_ctx->mutex);
16566  continue;
16567  }
16568  LRETURN;
16569  }
16570  }
16571 
16572 END:
16573 
16574  ni_pthread_mutex_unlock(&p_ctx->mutex);
16575 
16576  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16577 
16578  return retval;
16579 }
16580 
16581 /*!*****************************************************************************
16582  * \brief Get DDR configuration of Quadra device
16583  *
16584  * \param[in/out] p_ctx pointer to a session context with valid file handle
16585  *
16586  * \return On success NI_RETCODE_SUCCESS
16587  * On failure NI_RETCODE_INVALID_PARAM
16588  * NI_RETCODE_ERROR_MEM_ALOC
16589  * NI_RETCODE_ERROR_NVME_CMD_FAILED
16590  ******************************************************************************/
16592 {
16593  void *p_buffer = NULL;
16594  ni_nvme_identity_t *p_id_data;
16596  ni_event_handle_t event_handle = NI_INVALID_EVENT_HANDLE;
16597  uint32_t ui32LBA = IDENTIFY_DEVICE_R;
16598  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
16599 
16600  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16601 
16602  if (NI_INVALID_DEVICE_HANDLE == device_handle)
16603  {
16604  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
16605  __func__);
16606  retval = NI_RETCODE_INVALID_PARAM;
16607  LRETURN;
16608  }
16609 
16610  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
16612  {
16613  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
16614  NI_ERRNO, __func__);
16615  retval = NI_RETCODE_ERROR_MEM_ALOC;
16616  LRETURN;
16617  }
16618 
16619  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
16620 
16621  if (ni_nvme_send_read_cmd(device_handle, event_handle, p_buffer,
16622  NI_NVME_IDENTITY_CMD_DATA_SZ, ui32LBA) < 0)
16623  {
16625  LRETURN;
16626  }
16627 
16628  p_id_data = (ni_nvme_identity_t *) p_buffer;
16629 
16630  if (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR_4G)
16631  {
16632  p_ctx->ddr_config = 6;
16633  }
16634  else
16635  {
16636  if (ni_cmp_fw_api_ver(
16638  "6rJ") >= 0)
16639  {
16640  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
16641  ? 3 : ((p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_DR)? 4 : 5);
16642  }
16643  else if (ni_cmp_fw_api_ver(
16645  "6rD") >= 0)
16646  {
16647  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
16648  ? 3 : 4;
16649  } else
16650  {
16651  p_ctx->ddr_config = (p_id_data->memory_cfg == NI_QUADRA_MEMORY_CONFIG_SR)
16652  ? 1 : 2;
16653  }
16654  }
16655 
16656  ni_log2(p_ctx, NI_LOG_DEBUG, "Memory configuration %d\n",p_ctx->ddr_config);
16657 END:
16658 
16659  ni_aligned_free(p_buffer);
16660  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
16661 
16662  return retval;
16663 }
16664 
16665 /*!*****************************************************************************
16666  * \brief Set DDR configuration of Quadra device
16667  *
16668  * \param[in] p_ctx pointer to a session context with valid file handle
16669  * \param[in] ddr_priority_mode ddr priority mode
16670  *
16671  * \return On success NI_RETCODE_SUCCESS
16672  * On failure NI_RETCODE_INVALID_PARAM
16673  * NI_RETCODE_ERROR_MEM_ALOC
16674  * NI_RETCODE_ERROR_NVME_CMD_FAILED
16675  ******************************************************************************/
16677  uint8_t ddr_priority_mode)
16678 {
16679  void *p_buffer = NULL;
16681  ni_ddr_priority_config_t *p_cfg = NULL;
16682  uint32_t ui32LBA = CONFIG_SESSION_DDR_PRIORITY_W(p_ctx->session_id);
16683  ni_device_handle_t device_handle = p_ctx->blk_io_handle;
16684 
16685  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16686 
16687  ni_log2(p_ctx, NI_LOG_INFO, "set ddr priority %d\n",
16688  ddr_priority_mode);
16689 
16690  if (NI_INVALID_DEVICE_HANDLE == device_handle)
16691  {
16692  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR: %s(): invalid passed parameters\n",
16693  __func__);
16694  retval = NI_RETCODE_INVALID_PARAM;
16695  LRETURN;
16696  }
16697 
16698  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE),
16700  {
16701  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer.\n",
16702  NI_ERRNO, __func__);
16703  retval = NI_RETCODE_ERROR_MEM_ALOC;
16704  LRETURN;
16705  }
16706 
16707  memset(p_buffer, 0, NI_NVME_IDENTITY_CMD_DATA_SZ);
16708  p_cfg = (ni_ddr_priority_config_t *)p_buffer;
16709  p_cfg->ddr_mode = ddr_priority_mode;
16710  if (ni_nvme_send_write_cmd(p_ctx->blk_io_handle, NI_INVALID_EVENT_HANDLE,
16711  p_buffer,NI_DATA_BUFFER_LEN, ui32LBA) < 0)
16712  {
16713  ni_log2(p_ctx, NI_LOG_ERROR, "DDR priority setting failed with mode %d\n",
16714  ddr_priority_mode);
16716  LRETURN;
16717  }
16718 END:
16719  ni_aligned_free(p_buffer);
16720  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): retval: %d\n", __func__, retval);
16721 
16722  return retval;
16723 }
16724 
16725 /*!*****************************************************************************
16726  * \brief Allocate memory for the metadata header and auxillary data for
16727  * encoder input data.
16728  *
16729  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
16730  *
16731  * \param[in] extra_len Length header and auxillary data
16732  *
16733  * \return On success
16734  * NI_RETCODE_SUCCESS
16735  * On failure
16736  * NI_RETCODE_INVALID_PARAM
16737  * NI_RETCODE_ERROR_MEM_ALOC
16738  *****************************************************************************/
16740  int extra_len)
16741 {
16742  void *metadata_buffer = NULL;
16743  int retval = NI_RETCODE_SUCCESS;
16744 
16745  if ((!p_frame) || (extra_len <= 0))
16746  {
16748  "ERROR: %s passed parameters are null or not supported, "
16749  "p_frame %p, extra_len %d",
16750  __func__, p_frame, extra_len);
16751  return NI_RETCODE_INVALID_PARAM;
16752  }
16753 
16754  int buffer_size = extra_len;
16755  if (buffer_size % NI_MEM_PAGE_ALIGNMENT)
16756  {
16757  buffer_size = ((buffer_size / NI_MEM_PAGE_ALIGNMENT) + 1) *
16759  }
16760 
16761  // Check if previous metadata buffer needs to be freed
16762  if ((p_frame->metadata_buffer_size != buffer_size) &&
16763  (p_frame->metadata_buffer_size > 0))
16764  {
16766  "%s: free current p_frame metadata buffer, "
16767  "p_frame->buffer_size=%u\n",
16768  __func__, p_frame->metadata_buffer_size);
16769  p_frame->metadata_buffer_size = 0;
16771  }
16772 
16773  // Check if new metadata buffer needs to be allocated
16774  if (p_frame->metadata_buffer_size != buffer_size)
16775  {
16776  if (ni_posix_memalign(&metadata_buffer, sysconf(_SC_PAGESIZE),
16777  buffer_size))
16778  {
16780  "ERROR %d: %s() Cannot allocate metadata buffer.\n",
16781  NI_ERRNO, __func__);
16782  retval = NI_RETCODE_ERROR_MEM_ALOC;
16783  LRETURN;
16784  }
16785 
16786  // init once after allocation
16787  memset(metadata_buffer, 0, buffer_size);
16788  p_frame->metadata_buffer_size = buffer_size;
16789  p_frame->p_metadata_buffer = metadata_buffer;
16790 
16791  ni_log(NI_LOG_DEBUG, "%s: allocated new metadata buffer\n", __func__);
16792  } else
16793  {
16794  ni_log(NI_LOG_DEBUG, "%s: reuse metadata buffer\n", __func__);
16795  }
16796 
16798  "%s: success: p_frame->p_metadata_buffer %p "
16799  "p_frame->metadata_buffer_size=%u\n",
16800  __func__, p_frame->p_metadata_buffer, p_frame->metadata_buffer_size);
16801 
16802 END:
16803 
16804  if (NI_RETCODE_SUCCESS != retval)
16805  {
16806  ni_aligned_free(metadata_buffer);
16807  }
16808 
16809  return retval;
16810 }
16811 
16812 /*!*****************************************************************************
16813  * \brief Allocate memory for the non-4k-aligned part at the start of YUV data for
16814  * encoder input data.
16815  *
16816  * \param[in] p_frame Pointer to a caller allocated ni_frame_t struct
16817  *
16818  * \param[in] start_len Length of non-4k-aligned part at the start of YUV data
16819  *
16820  * \return On success
16821  * NI_RETCODE_SUCCESS
16822  * On failure
16823  * NI_RETCODE_INVALID_PARAM
16824  * NI_RETCODE_ERROR_MEM_ALOC
16825  *****************************************************************************/
16827 {
16828  void *start_buffer = NULL;
16829  int retval = NI_RETCODE_SUCCESS;
16830 
16831  if (!p_frame)
16832  {
16834  "ERROR: %s passed parameters are null or not supported, "
16835  "p_frame %p\n",
16836  __func__, p_frame);
16837  return NI_RETCODE_INVALID_PARAM;
16838  }
16839 
16840  // Check if new start buffer needs to be allocated
16841  if (!p_frame->start_buffer_size)
16842  {
16843  if (ni_posix_memalign(&start_buffer, sysconf(_SC_PAGESIZE),
16845  {
16847  "ERROR %d: %s() Cannot allocate start buffer.\n",
16848  NI_ERRNO, __func__);
16849  retval = NI_RETCODE_ERROR_MEM_ALOC;
16850  LRETURN;
16851  }
16852 
16853  // init once after allocation
16856  p_frame->p_start_buffer = start_buffer;
16857 
16858  ni_log(NI_LOG_DEBUG, "%s: allocated new start buffer\n", __func__);
16859  } else
16860  {
16861  ni_log(NI_LOG_DEBUG, "%s: reuse start buffer\n", __func__);
16862  }
16863 
16865  "%s: success: p_frame->p_start_buffer %p "
16866  "p_frame->start_buffer_size=%u\n",
16867  __func__, p_frame->p_start_buffer, p_frame->start_buffer_size);
16868 
16869 END:
16870 
16871  if (NI_RETCODE_SUCCESS != retval)
16872  {
16873  ni_aligned_free(start_buffer);
16874  }
16875 
16876  return retval;
16877 }
16878 
16880  ni_network_perf_metrics_t *p_metrics)
16881 {
16883  void *p_buffer = NULL;
16884  uint32_t dataLen;
16885  uint32_t ui32LBA = 0;
16886 
16887  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): enter\n", __func__);
16888  if (!p_ctx || !p_metrics)
16889  {
16890  ni_log2(p_ctx, NI_LOG_ERROR,
16891  "ERROR: %s() passed parameters are null!, return\n", __func__);
16892  retval = NI_RETCODE_INVALID_PARAM;
16893  LRETURN;
16894  }
16895 
16897  "6N") >= 0)
16898  {
16899  dataLen =
16901  ~(NI_MEM_PAGE_ALIGNMENT - 1);
16902  if (ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), dataLen))
16903  {
16904  ni_log2(p_ctx, NI_LOG_ERROR, "ERROR %d: %s() Cannot allocate buffer\n",
16905  NI_ERRNO, __func__);
16906  retval = NI_RETCODE_ERROR_MEM_ALOC;
16907  LRETURN;
16908  }
16909  memset(p_buffer, 0, sizeof(ni_network_perf_metrics_t));
16910 
16911  ui32LBA =
16913  retval =
16915  p_buffer, dataLen, ui32LBA);
16916  if ((int32_t)retval != NI_RETCODE_SUCCESS)
16917  {
16918  ni_log2(p_ctx, NI_LOG_ERROR, "%s(): NVME command Failed\n", __func__);
16920  LRETURN;
16921  }
16922  CHECK_ERR_RC(p_ctx, retval, 0, nvme_cmd_xcoder_read, p_ctx->device_type,
16923  p_ctx->hw_id, &(p_ctx->session_id), OPT_1);
16924 
16925  *p_metrics = *((ni_network_perf_metrics_t *)p_buffer);
16926  } else
16927  {
16928  p_metrics->total_cycles = (uint32_t)(-1);
16929  p_metrics->total_idle_cycles = (uint32_t)(-1);
16930  }
16931 END:
16932 
16933  ni_aligned_free(p_buffer);
16934  ni_log2(p_ctx, NI_LOG_TRACE, "%s(): exit\n", __func__);
16935 
16936  return retval;
16937 }
16938 
16939 /*!*****************************************************************************
16940  * \brief Send namespace num / Opmode and SRIOv index/value to the device with
16941  * specified logic block address.
16942  *
16943  * \param[in] device_handle Device handle obtained by calling ni_device_open
16944  * \param[in] Key Represents either namespace num or opmode
16945  * \param[in] Value Represents either SRIOv index or opmode value
16946  *
16947  * \return On success
16948  * NI_RETCODE_SUCCESS
16949  * On failure
16950  * NI_RETCODE_ERROR_MEM_ALOC
16951  * NI_RETCODE_ERROR_NVME_CMD_FAILED
16952  ******************************************************************************/
16953 ni_retcode_t ni_device_config_ns_qos(ni_device_handle_t device_handle,
16954  uint32_t key,
16955  uint32_t value)
16956 {
16957  char buf[NI_DATA_BUFFER_LEN] = {'\0'};
16958  buf[0] = (uint8_t)key;
16959  buf[1] = (uint8_t)value;
16960  // event handle could be ignored
16961  return ni_nvme_send_write_cmd(device_handle, NI_INVALID_EVENT_HANDLE,
16962  (void *)buf, NI_DATA_BUFFER_LEN,
16964 }
16965 
16967 {
16968  char *CoreName;
16969  switch (eCoreType)
16970  {
16971  case ALL_CORE:
16972  CoreName = (char *)"all";
16973  break;
16974  case NVME_CORE:
16975  CoreName = (char *)"np";
16976  break;
16977  case EP_CORE:
16978  CoreName = (char *)"ep";
16979  break;
16980  case DP_CORE:
16981  CoreName = (char *)"dp";
16982  break;
16983  case TP_CORE:
16984  CoreName = (char *)"tp";
16985  break;
16986  case FP_CORE:
16987  CoreName = (char *)"fp";
16988  break;
16989  default:
16990  CoreName = (char *)"Not Found";
16991  break;
16992  }
16993  return CoreName;
16994 }
16995 
16996 uint32_t ni_get_log_lba(ni_core_type_t eCoreType)
16997 {
16998  uint32_t lba;
16999  switch (eCoreType)
17000  {
17001  case NVME_CORE:
17002  lba = NVME_LOG_OFFSET_IN_4K;
17003  break;
17004  case EP_CORE:
17005  lba = EP_LOG_OFFSET_IN_4K;
17006  break;
17007  case DP_CORE:
17008  lba = DP_LOG_OFFSET_IN_4K;
17009  break;
17010  case TP_CORE:
17011  lba = TP_LOG_OFFSET_IN_4K;
17012  break;
17013  case FP_CORE:
17014  lba = FP_LOG_OFFSET_IN_4K;
17015  break;
17016  default:
17017  ni_log(NI_LOG_ERROR, "%s:() Invalid core ID:%u\n", __func__, eCoreType);
17018  lba = 0;
17019  break;
17020  }
17021 
17022  return lba;
17023 }
17024 
17025 ni_retcode_t ni_dump_log_single_core(ni_session_context_t *p_ctx, void* p_data, uint32_t core_id, bool gen_log_file)
17026 {
17027  int32_t rc;
17028  uint32_t lba = 0;
17029  uint32_t data_len = CPU_LOG_BUFFER_SIZE;
17030  char *core_name = NULL;
17031  FILE *p_file = NULL;
17033 
17034  if (!p_ctx || !p_data)
17035  {
17036  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
17037  __func__, __LINE__, p_ctx, p_data);
17038  return NI_RETCODE_INVALID_PARAM;
17039  }
17040 
17041  memset(p_data, 0, CPU_LOG_BUFFER_SIZE);
17042  *(uint8_t *)p_data = 0x55;
17043 
17044  lba = ni_get_log_lba((ni_core_type_t)core_id);
17045  if (lba == 0)
17046  {
17047  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR core_id %u\n",
17048  __func__, __LINE__, core_id);
17049  return NI_RETCODE_INVALID_PARAM;
17050  }
17051 
17052  core_name = ni_get_core_name((ni_core_type_t)core_id);
17053 
17054  rc = ni_nvme_send_read_cmd(p_ctx->blk_io_handle, p_ctx->event_handle, p_data, data_len, lba);
17055 
17056  if (rc != NI_RETCODE_SUCCESS)
17057  {
17058  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR %d: nvme read %s core failed\n",
17059  __func__, __LINE__, rc, core_name);
17060  }
17061  else if (gen_log_file)
17062  {
17063  //generate log file e.g. raw_dp_slot_0_0000.bin
17064  char filename[32] = "raw_";
17065  strcat(filename, core_name);
17066  strcat(filename, "_slot_");
17067 #ifdef __linux__
17068  bool pcie_id_name = false;
17069  char devFilePath[1024] = {0};
17070  char devFDPath[1024] = {0};
17071  char pcie[64] = {0};
17072  char domain[5] = {0}, slot[3] = {0}, dev[3] = {0}, func[2] = {0};
17073  //p_ctx->blk_dev_name might be empty so look up the file name
17074  snprintf(devFDPath, sizeof(devFDPath), "/proc/self/fd/%d", p_ctx->blk_io_handle);
17075  ssize_t len = readlink(devFDPath, devFilePath, sizeof(devFilePath)-1);
17076  if (len != -1) {
17077  devFilePath[len] = '\0';
17078  }
17079  if (strstr(devFilePath, "/dev/nvme") != NULL)
17080  {
17081  get_dev_pcie_addr(devFilePath, pcie, domain, slot, dev, func);
17082  if (strlen(pcie) > 0 && strlen(slot) > 0 && strlen(domain) > 0)
17083  {
17084  strcat(filename, slot);
17085  strcat(filename, "_");
17086  strcat(filename, domain);
17087  pcie_id_name = true;
17088  }
17089  }
17090  if (!pcie_id_name)
17091  {
17092  ni_log2(p_ctx, NI_LOG_INFO, "%s:():%d: For dev %d can't look up PCI domain and slot info. Defaulting to slot=hw_id and domain=0000\n",
17093  __func__, __LINE__, p_ctx->blk_io_handle);
17094  char num[4] = {0};
17095  snprintf(num, 4, "%d", p_ctx->hw_id);
17096  strcat(filename, num);
17097  strcat(filename, "_0000");
17098  }
17099  strcat(filename, ".bin");
17100  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d %s core %s creating file %s\n",
17101  p_ctx->blk_io_handle, devFilePath, core_name, filename);
17102 #else
17103  char num[4] = {0};
17104  snprintf(num, 4, "%02x", p_ctx->hw_id);
17105  strcat(filename, num);
17106  strcat(filename, "_0000");
17107  strcat(filename, ".bin");
17108  ni_log2(p_ctx, NI_LOG_INFO, "For dev %d core %s creating file %s\n",
17109  p_ctx->blk_io_handle, core_name, filename);
17110 #endif
17111  p_file = fopen(filename, "wb");
17112  if (p_file)
17113  {
17114  /* Write out the stream header */
17115  if (fwrite((uint8_t *)p_data ,
17116  data_len, 1, p_file) != 1)
17117  {
17118  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: writing data %u bytes error!\n",
17119  __func__, __LINE__, data_len);
17120  ni_log2(p_ctx, NI_LOG_ERROR, "Error: ferror rc = %d\n", ferror(p_file));
17121  retval = NI_RETCODE_FAILURE;
17122  }
17123  if (fflush(p_file))
17124  {
17125  ni_log2(p_ctx, NI_LOG_ERROR, "Error: writing data frame flush failed! errno %d\n",
17126  ferror(p_file));
17127  retval = NI_RETCODE_FAILURE;
17128  }
17129  fclose(p_file);
17130  }
17131  else
17132  {
17133  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: Error: cannot open %s\n",
17134  __func__, __LINE__, filename);
17135  retval = NI_RETCODE_FAILURE;
17136  }
17137  }
17138 
17139  return retval;
17140 }
17141 
17142 ni_retcode_t ni_dump_log_all_cores(ni_session_context_t *p_ctx, void* p_data, bool gen_log_file)
17143 {
17144  int i = 1;
17145 
17146  if (!p_ctx || !p_data)
17147  {
17148  ni_log2(p_ctx, NI_LOG_ERROR, "%s:():%d: ERROR invalid pointer p_ctx %p p_data %p\n",
17149  __func__, __LINE__, p_ctx, p_data);
17150  return NI_RETCODE_INVALID_PARAM;
17151  }
17152 
17153  for (i = NVME_CORE; i< NUM_OF_CORES; i++)
17154  {
17155  ni_dump_log_single_core(p_ctx, p_data, i, gen_log_file);
17156  p_data = (void*)((uint8_t *)p_data + CPU_LOG_BUFFER_SIZE);
17157  }
17158 
17159  return NI_RETCODE_SUCCESS;
17160 }
17161 
17163  niFrameSurface1_t *source,
17164  uint64_t ui64DestAddr,
17165  uint32_t ui32FrameSize)
17166 {
17167  void *p_buffer = NULL;
17168  ni_retcode_t retval;
17169  uint32_t ui32LBA;
17170  uint8_t *p_data;
17171  uint16_t ui16Direction = NI_P2P_SEND;
17172 
17173  /* allocate memory aligned buffer */
17174  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
17175  if (retval != 0)
17176  {
17177  retval = NI_RETCODE_ERROR_MEM_ALOC;
17178  LRETURN;
17179  }
17180 
17181  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17182 
17183  p_data = (uint8_t *) p_buffer;
17184 
17185  /* Add payload */
17186  memcpy(p_data, &ui64DestAddr, sizeof(uint64_t));
17187  memcpy(p_data + 8, &ui32FrameSize, sizeof(uint32_t));
17188  memcpy(p_data + 12, &source->ui16FrameIdx, sizeof(uint16_t));
17189  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
17190  memcpy(p_data + 18, &ui16Direction, sizeof(uint16_t));
17191 
17193 
17194  retval = ni_nvme_send_write_cmd((ni_device_handle_t)(int64_t)source->device_handle,
17195  NI_INVALID_DEVICE_HANDLE, p_buffer,
17196  NI_DATA_BUFFER_LEN, ui32LBA);
17197 
17198  if (retval < 0)
17199  {
17200  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed %d\n", __func__, retval);
17201  ni_aligned_free(p_buffer);
17203  LRETURN;
17204  }
17205 
17206 END:
17207  if (p_buffer != NULL)
17208  ni_aligned_free(p_buffer);
17209  return retval;
17210 }
17211 
17213  ni_session_context_t *p_ctx,
17214  const ni_p2p_sgl_t *dmaAddrs,
17215  ni_frame_t *pDstFrame)
17216 {
17217  void *p_buffer = NULL;
17218  ni_retcode_t retval;
17219  uint32_t ui32LBA = 0;
17220  niFrameSurface1_t *pSurface;
17221  uint8_t *p_data;
17222  uint16_t ui16Direction = NI_P2P_RECV;
17223  uint32_t ui32Dummy = 0;
17224  int i;
17225 
17226  if (dmaAddrs->ui32NumEntries > NI_MAX_P2P_SGL_ENTRIES)
17227  {
17228  ni_log2(p_ctx, NI_LOG_ERROR, "Too many SGL entries\n");
17229  retval = NI_RETCODE_INVALID_PARAM;
17230  LRETURN;
17231  }
17232 
17233  /* allocate memory aligned buffer */
17234  retval = ni_posix_memalign(&p_buffer, sysconf(_SC_PAGESIZE), NI_DATA_BUFFER_LEN);
17235  if (retval != 0)
17236  {
17237  retval = NI_RETCODE_ERROR_MEM_ALOC;
17238  LRETURN;
17239  }
17240 
17241  pSurface = (niFrameSurface1_t *) pDstFrame->p_data[3];
17242 
17243  memset(p_buffer, 0, NI_DATA_BUFFER_LEN);
17244 
17245  p_data = (uint8_t *) p_buffer;
17246 
17247  /* Add payload */
17248  memcpy(p_data, &dmaAddrs->ui64DMAAddr[0], 8); // dummy data, not used for p2p read
17249  memcpy(p_data + 8, &ui32Dummy, 4); // dummy data, not used for p2p read
17250  memcpy(p_data + 12, &pSurface->ui16FrameIdx, 2); // dummy data, not used for p2p read
17251  memcpy(p_data + 14, MAGIC_P2P_VALUE, 4);
17252  memcpy(p_data + 18, &ui16Direction, 2);
17253  memcpy(p_data + 20, &dmaAddrs->ui32NumEntries, 4);
17254 
17255  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
17256  {
17257  memcpy(&p_data[24] + (i*8), &dmaAddrs->ui64DMAAddr[i], 8);
17258  }
17259 
17260  for (i = 0; i < dmaAddrs->ui32NumEntries; i++)
17261  {
17262  memcpy(&p_data[24] + (dmaAddrs->ui32NumEntries * 8) + (i * 4), &dmaAddrs->ui32DMALen[i], 4);
17263  }
17264 
17266 
17267  retval = ni_nvme_send_write_cmd(p_ctx->blk_io_handle, p_ctx->event_handle,
17268  p_buffer, NI_DATA_BUFFER_LEN, ui32LBA);
17269 
17270  if (retval < 0)
17271  {
17272  ni_log2(p_ctx, NI_LOG_ERROR, "%s: NVME command failed\n", __func__);
17273  ni_aligned_free(p_buffer);
17275  LRETURN;
17276  }
17277 
17278 END:
17279  if (p_buffer != NULL)
17280  ni_aligned_free(p_buffer);
17281  return retval;
17282 }
17283 
17284 int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
17285 {
17286  return (pQuery->total_pixel_load < ui32CurrentLowest) ? 1 : 0;
17287 }
_ni_t408_config_t::maxQpB
int32_t maxQpB
Definition: ni_device_api_priv.h:482
_ni_metadata_enc_bstream::ui16psnr_y
uint16_t ui16psnr_y
Definition: ni_device_api_priv.h:296
_ni_metadata_enc_bstream::metadata_size
uint32_t metadata_size
Definition: ni_device_api_priv.h:275
TUNE_BFRAME_VISUAL_MEDIUM
#define TUNE_BFRAME_VISUAL_MEDIUM
Definition: ni_device_api_priv.h:309
ni_decoder_output_config_t::ui8CropMode
uint8_t ui8CropMode
Definition: ni_device_api_priv.h:691
_ni_encoder_config_t::ui16rootBufId
uint16_t ui16rootBufId
Definition: ni_device_api_priv.h:556
_ni_xcoder_params::reconf_demo_mode
int reconf_demo_mode
Definition: ni_device_api.h:2626
_ni_encoder_config_t::ui16hdr10_dx2
uint16_t ui16hdr10_dx2
Definition: ni_device_api_priv.h:589
NI_RETCODE_PARAM_ERROR_CONF_WIN_BOT
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_BOT
Definition: ni_defs.h:460
NI_MAX_FRAMERATE
#define NI_MAX_FRAMERATE
Definition: ni_device_api.h:116
_ni_encoder_cfg_params::enable_mb_level_rc
int enable_mb_level_rc
Definition: ni_device_api.h:2303
_ni_encoder_cfg_params::frame_rate
int frame_rate
Definition: ni_device_api.h:2167
ni_decoder_output_picture_size::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:685
_ni_frame_config::picture_width
uint16_t picture_width
Definition: ni_device_api.h:2704
_ni_encoder_config_t::ui32hdr10_minluma
uint32_t ui32hdr10_minluma
Definition: ni_device_api_priv.h:594
NI_RETCODE_ERROR_VPU_RECOVERY
@ NI_RETCODE_ERROR_VPU_RECOVERY
Definition: ni_defs.h:508
_ni_xcoder_params::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api.h:2614
_ni_metadata_enc_frame::force_pic_qp_b
uint16_t force_pic_qp_b
Definition: ni_device_api_priv.h:251
ni_decoder_output_config_t::ui8Force8Bit
uint8_t ui8Force8Bit
Definition: ni_device_api_priv.h:690
_ni_nvme_identity::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_nvme.h:220
ni_lat_meas.h
Utility definitions for measuring frame/packet processing time in NETINT video processing devices.
_ni_encoder_config_t::u8skipFrameInterval
uint8_t u8skipFrameInterval
Definition: ni_device_api_priv.h:598
ni_send_to_target
ni_retcode_t ni_send_to_target(ni_session_context_t *p_ctx, niFrameSurface1_t *source, uint64_t ui64DestAddr, uint32_t ui32FrameSize)
Definition: ni_device_api_priv.c:17162
_ni_t408_config_t::saoEnable
int32_t saoEnable
Definition: ni_device_api_priv.h:397
_ni_xcoder_params::luma_linesize
int luma_linesize
Definition: ni_device_api.h:2679
ni_pthread_mutex_unlock
int ni_pthread_mutex_unlock(ni_pthread_mutex_t *mutex)
thread mutex unlock
Definition: ni_util.c:4069
NI_PIX_FMT_BGRA
@ NI_PIX_FMT_BGRA
Definition: ni_device_api.h:267
CLEAR_INSTANCE_BUF_W
#define CLEAR_INSTANCE_BUF_W(frame_id)
Definition: ni_nvme.h:869
_ni_t35_sei_mesg_type
_ni_t35_sei_mesg_type
Definition: ni_device_api_priv.c:86
_ni_load_query::fw_p2p_mem_usage
uint32_t fw_p2p_mem_usage
Definition: ni_device_api.h:1165
_ni_sei_header::size
uint16_t size
Definition: ni_device_api.h:333
CONFIG_SESSION_KeepAliveTimeout_W
#define CONFIG_SESSION_KeepAliveTimeout_W(sid)
Definition: ni_nvme.h:873
ni_config_instance_flush
ni_retcode_t ni_config_instance_flush(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a p_config command to flush the stream.
Definition: ni_device_api_priv.c:6776
NI_CODEC_FORMAT_JPEG
@ NI_CODEC_FORMAT_JPEG
Definition: ni_device_api.h:881
_ni_encoder_cfg_params::ver_offset
int ver_offset
Definition: ni_device_api.h:2317
ni_decoder_output_picture_size::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:684
NI_QUADRA_MEMORY_CONFIG_DR
#define NI_QUADRA_MEMORY_CONFIG_DR
Definition: ni_device_api_priv.h:781
_ni_dec_mastering_display_colour_volume_bytes::display_primaries
uint16_t display_primaries[3][2]
Definition: ni_device_api.h:1030
_ni_nvme_identity::memory_cfg
uint8_t memory_cfg
Definition: ni_nvme.h:225
NI_P2P_SEND
#define NI_P2P_SEND
Definition: ni_device_api_priv.h:787
ni_device_get_ddr_configuration
ni_retcode_t ni_device_get_ddr_configuration(ni_session_context_t *p_ctx)
Get DDR configuration of Quadra device.
Definition: ni_device_api_priv.c:16591
ni_encoder_session_write
int ni_encoder_session_write(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Send a YUV p_frame to encoder.
Definition: ni_device_api_priv.c:3953
_ni_device_capability::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_device_api.h:1121
_ni_t408_config_t::pu16IntraDcDeltaRate
int32_t pu16IntraDcDeltaRate
Definition: ni_device_api_priv.h:462
_ni_session_statistic_t::ui32InstErrors
uint32_t ui32InstErrors
Definition: ni_defs.h:379
NI_MAX_RESOLUTION_HEIGHT
#define NI_MAX_RESOLUTION_HEIGHT
Definition: ni_device_api.h:101
_ni_decoder_input_params_t::mcmode
int mcmode
Definition: ni_device_api.h:2405
_ni_t408_config_t::losslessEnable
int32_t losslessEnable
Definition: ni_device_api_priv.h:370
_ni_encoder_config_t::ui32ltrFirstGap
uint32_t ui32ltrFirstGap
Definition: ni_device_api_priv.h:565
NI_DEVICE_TYPE_ENCODER
@ NI_DEVICE_TYPE_ENCODER
Definition: ni_defs.h:342
_ni_t408_config_t::tier
int32_t tier
Definition: ni_device_api_priv.h:368
_ni_frame::separate_start
uint8_t separate_start
Definition: ni_device_api.h:2603
_ni_nvme_identity::xcoder_cnt
uint8_t xcoder_cnt[14]
Definition: ni_nvme.h:231
_ni_packet::p_custom_sei_set
ni_custom_sei_set_t * p_custom_sei_set
Definition: ni_device_api.h:2747
_ni_encoder_config_t::ui8enable2PassGopPatern
uint8_t ui8enable2PassGopPatern
Definition: ni_device_api_priv.h:608
INST_BUF_INFO_R_ACQUIRE
@ INST_BUF_INFO_R_ACQUIRE
Definition: ni_device_api_priv.h:53
_ni_p2p_sgl_t
Definition: ni_defs.h:396
_ni_t408_config_t::strongIntraSmoothEnable
uint32_t strongIntraSmoothEnable
Definition: ni_device_api_priv.h:440
_ni_instance_mgr_general_status::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api_priv.h:91
_ni_frame::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2563
_ni_network_data
Definition: ni_device_api.h:1314
NI_CC_SEI_BYTE0
#define NI_CC_SEI_BYTE0
Definition: ni_device_api.h:431
_ni_session_context::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:1545
_ni_session_context::p_all_zero_buf
void * p_all_zero_buf
Definition: ni_device_api.h:1522
NI_HDR10P_SEI_BYTE1
#define NI_HDR10P_SEI_BYTE1
Definition: ni_device_api.h:440
ni_config_instance_set_encoder_params
ni_retcode_t ni_config_instance_set_encoder_params(ni_session_context_t *p_ctx)
Send a p_config command to configure encoding parameters.
Definition: ni_device_api_priv.c:6974
_ni_session_context::auto_dl_handle
ni_device_handle_t auto_dl_handle
Definition: ni_device_api.h:1420
_ni_thread_arg_struct_t::p_mutex
ni_pthread_mutex_t * p_mutex
Definition: ni_device_api.h:1218
_ni_metadata_enc_bstream::gop_size
uint8_t gop_size
Definition: ni_device_api_priv.h:294
_ni_encoder_cfg_params::skip_frame_interval
int skip_frame_interval
Definition: ni_device_api.h:2338
NI_MIN_RESOLUTION_WIDTH
#define NI_MIN_RESOLUTION_WIDTH
Definition: ni_device_api.h:86
_ni_decoder_config_t::ui8MaxExtraHwFrameCnt
uint8_t ui8MaxExtraHwFrameCnt
Definition: ni_device_api_priv.h:715
NI_POOL_TYPE_P2P
@ NI_POOL_TYPE_P2P
Definition: ni_device_api.h:472
NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
@ NI_H265_USERDATA_FLAG_MASTERING_COLOR_VOL
Definition: ni_device_api.h:357
NI_FW_ENC_BITSTREAM_META_DATA_SIZE
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE
Definition: ni_device_api_priv.h:796
XCODER_FRAME_OFFSET_DIFF_THRES
#define XCODER_FRAME_OFFSET_DIFF_THRES
Definition: ni_util.h:79
_ni_metadata_enc_bstream
Definition: ni_device_api_priv.h:273
_ni_session_config_rw::ui8Enable
uint8_t ui8Enable
Definition: ni_device_api_priv.h:70
_ni_hw_capability::codec_type
uint8_t codec_type
Definition: ni_device_api.h:1095
_ni_encoder_cfg_params::enable_dynamic_32x32_merge
int enable_dynamic_32x32_merge
Definition: ni_device_api.h:2271
_ni_encoder_cfg_params::hor_offset
int hor_offset
Definition: ni_device_api.h:2316
_ni_encoder_cfg_params::inLoopDSRatio
int inLoopDSRatio
Definition: ni_device_api.h:2257
_ni_session_context::max_retry_fail_count
int max_retry_fail_count[2]
Definition: ni_device_api.h:1652
_ni_metadata_enc_bstream_rev61::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:269
ni_config_session_rw
ni_retcode_t ni_config_session_rw(ni_session_context_t *p_ctx, ni_session_config_rw_type_t rw_type, uint8_t enable, uint8_t hw_action, uint16_t frame_id)
Configure the read/write pipe for a session to control its behavior.
Definition: ni_device_api_priv.c:6574
_ni_content_light_level_info_bytes
payload format of HDR SEI content light level info
Definition: ni_device_api.h:1041
ni_decoder_session_read_desc
ni_retcode_t ni_decoder_session_read_desc(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Retrieve a hw desc p_frame from decoder.
Definition: ni_device_api_priv.c:13264
_ni_encoder_config_t::ui8tuneBframeVisual
uint8_t ui8tuneBframeVisual
Definition: ni_device_api_priv.h:637
_ni_scaler_config
Definition: ni_device_api_priv.h:743
ni_frame_buffer_free
ni_retcode_t ni_frame_buffer_free(ni_frame_t *p_frame)
Free frame buffer that was previously allocated with either ni_frame_buffer_alloc or ni_encoder_frame...
Definition: ni_device_api.c:3491
QUERY_INSTANCE_UPLOAD_ID_R
#define QUERY_INSTANCE_UPLOAD_ID_R(sid, instance)
Definition: ni_nvme.h:738
_ni_nvme_identity::hw3_codec_format
uint8_t hw3_codec_format
Definition: ni_nvme.h:208
_ni_encoder_config_t::ui16HDR10AveLight
uint16_t ui16HDR10AveLight
Definition: ni_device_api_priv.h:540
NI_MAX_4K_FPS_QUADRA
#define NI_MAX_4K_FPS_QUADRA
Definition: ni_defs.h:232
_ni_t408_config_t::minQpP
int32_t minQpP
Definition: ni_device_api_priv.h:479
_ni_frame::vui_len
unsigned int vui_len
Definition: ni_device_api.h:2546
_ni_frame::sei_hdr_content_light_level_info_offset
unsigned int sei_hdr_content_light_level_info_offset
Definition: ni_device_api.h:2533
QUERY_SESSION_STATS_R
#define QUERY_SESSION_STATS_R(sid, instance)
Definition: ni_nvme.h:706
GC620_ABGR8888
#define GC620_ABGR8888
Definition: ni_device_api.h:202
_ni_encoder_frame_params::picture_type
uint16_t picture_type
Definition: ni_device_api_priv.h:200
_ni_decoder_config_t
Definition: ni_device_api_priv.h:705
_ni_instance_buf_info::hw_inst_ind
ni_instance_upload_ret_hwdesc_t hw_inst_ind
Definition: ni_device_api_priv.h:192
_ni_decoder_input_params_t::enable_ppu_scale_adapt
int enable_ppu_scale_adapt
Definition: ni_device_api.h:2428
_ni_encoder_cfg_params::pbRatio
float pbRatio
Definition: ni_device_api.h:2324
_ni_session_statistic_t::ui16ErrorCount
uint16_t ui16ErrorCount
Definition: ni_defs.h:382
_ni_encoder_config_t::i32tolCtbRcInter
int32_t i32tolCtbRcInter
Definition: ni_device_api_priv.h:572
ni_scaler_config_frame
ni_retcode_t ni_scaler_config_frame(ni_session_context_t *p_ctx, ni_frame_config_t *p_cfg)
config a frame in the scaler
Definition: ni_device_api_priv.c:5511
_ni_decoder_input_params_t::enable_out1
int enable_out1
Definition: ni_device_api.h:2403
ni_config_instance_sos
ni_retcode_t ni_config_instance_sos(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a p_config command for Start Of Stream.
Definition: ni_device_api_priv.c:6667
_ni_network_buffer::ui16Option
uint16_t ui16Option
Definition: ni_device_api_priv.h:737
_ni_network_layer_info::in_param
ni_network_layer_params_t * in_param
Definition: ni_device_api.h:1305
_ni_t408_config_t::disableDeblk
int32_t disableDeblk
Definition: ni_device_api_priv.h:392
_ni_packet::pts
long long pts
Definition: ni_device_api.h:2722
_ni_instance_mgr_stream_complete
Definition: ni_device_api_priv.h:175
_ni_encoder_config_t::i32vbvBufferSize
int32_t i32vbvBufferSize
Definition: ni_device_api_priv.h:511
_ni_session_config_rw::ui16ReadFrameId
uint16_t ui16ReadFrameId
Definition: ni_device_api_priv.h:74
ni_instance_mgr_detail_status_v1_t
struct _ni_instance_mgr_detail_status_v1 ni_instance_mgr_detail_status_v1_t
_ni_t408_config_t::nrInterWeightY
uint32_t nrInterWeightY
Definition: ni_device_api_priv.h:427
QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP
#define QUERY_INSTANCE_WBUFF_SIZE_R_BY_EP(sid, instance)
Definition: ni_nvme.h:726
_ni_encoder_cfg_params::enable_filler
int enable_filler
Definition: ni_device_api.h:2295
NI_RETCODE_PARAM_ERROR_RCENABLE
@ NI_RETCODE_PARAM_ERROR_RCENABLE
Definition: ni_defs.h:469
_ni_session_context::itu_t_t35_cc_sei_hdr_h264
uint8_t itu_t_t35_cc_sei_hdr_h264[NI_CC_SEI_HDR_H264_LEN]
Definition: ni_device_api.h:1369
_ni_t408_config_t::cu32MergeDeltaRate
int32_t cu32MergeDeltaRate
Definition: ni_device_api_priv.h:475
ni_rsrc_get_device_context
LIB_API ni_device_context_t * ni_rsrc_get_device_context(ni_device_type_t type, int guid)
Allocates and returns a pointer to ni_device_context_t struct based on provided device_type and guid....
NI_PARAM_AV1_MAX_AREA
#define NI_PARAM_AV1_MAX_AREA
Definition: ni_device_api.h:147
_ni_session_context::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:1650
QUERY_INSTANCE_HW_OUT_SIZE_R
#define QUERY_INSTANCE_HW_OUT_SIZE_R(sid, instance)
Definition: ni_nvme.h:746
_ni_instance_upload_ret_hwdesc::buffer_avail
int16_t buffer_avail
Definition: ni_device_api_priv.h:183
ni_ai_alloc_dst_frame
ni_retcode_t ni_ai_alloc_dst_frame(ni_session_context_t *p_ctx, niFrameSurface1_t *p_out_surface)
Definition: ni_device_api_priv.c:16199
_ni_instance_mgr_general_status::fw_share_mem_usage
uint8_t fw_share_mem_usage
Definition: ni_device_api_priv.h:94
XCODER_MIN_ENC_PIC_WIDTH
#define XCODER_MIN_ENC_PIC_WIDTH
Definition: ni_util.h:86
NI_CC_SEI_BYTE3
#define NI_CC_SEI_BYTE3
Definition: ni_device_api.h:434
NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
@ NI_RETCODE_NVME_SC_RESOURCE_UNAVAILABLE
Definition: ni_defs.h:529
_ni_encoder_cfg_params::gopLowdelay
int gopLowdelay
Definition: ni_device_api.h:2252
CONFIG_SESSION_DDR_PRIORITY_W
#define CONFIG_SESSION_DDR_PRIORITY_W(sid)
Definition: ni_nvme.h:890
ni_timestamp_register
ni_retcode_t ni_timestamp_register(ni_queue_buffer_pool_t *p_buffer_pool, ni_timestamp_table_t *p_table, int64_t timestamp, uint64_t data_info)
Register timestamp in timestamp/frameoffset table.
Definition: ni_util.c:1355
_ni_nvme_identity::hw3_video_level
uint8_t hw3_video_level
Definition: ni_nvme.h:215
CLOSE_SESSION_R
#define CLOSE_SESSION_R(sid, instance)
Definition: ni_nvme.h:700
ni_decode_cropping_rectangle::ui16W
uint16_t ui16W
Definition: ni_device_api_priv.h:679
_ni_frame_config::rectangle_x
int16_t rectangle_x
Definition: ni_device_api.h:2710
_ni_encoder_config_t::ui32hdr10_maxluma
uint32_t ui32hdr10_maxluma
Definition: ni_device_api_priv.h:593
_ni_gop_rps::ref_pic_used
int ref_pic_used
Definition: ni_device_api.h:1898
_ni_load_query::current_load
uint32_t current_load
Definition: ni_device_api.h:1152
_ni_session_context::dev_xcoder_name
char dev_xcoder_name[MAX_CHAR_IN_DEVICE_NAME]
Definition: ni_device_api.h:1439
_ni_packet::recycle_index
int recycle_index
Definition: ni_device_api.h:2730
_ni_load_query::total_contexts
uint32_t total_contexts
Definition: ni_device_api.h:1155
_ni_t408_config_t::cu08IntraDeltaRate
int32_t cu08IntraDeltaRate
Definition: ni_device_api_priv.h:467
_ni_session_context::frame_time_q
void * frame_time_q
Definition: ni_device_api.h:1364
NI_CC_SEI_BYTE2
#define NI_CC_SEI_BYTE2
Definition: ni_device_api.h:433
_ni_encoder_cfg_params::statistic_output_level
int statistic_output_level
Definition: ni_device_api.h:2335
_ni_dec_mastering_display_colour_volume_bytes::white_point_y
uint16_t white_point_y
Definition: ni_device_api.h:1032
_ni_session_statistic_t
Definition: ni_defs.h:369
_ni_metadata_enc_bstream::ssimY
uint32_t ssimY
Definition: ni_device_api_priv.h:283
ni_device_config_ns_qos
ni_retcode_t ni_device_config_ns_qos(ni_device_handle_t device_handle, uint32_t key, uint32_t value)
Send namespace num / Opmode and SRIOv index/value to the device with specified logic block address.
Definition: ni_device_api_priv.c:16953
_ni_encoder_config_t::ui16gdrDuration
uint16_t ui16gdrDuration
Definition: ni_device_api_priv.h:548
_ni_session_context::itu_t_t35_hdr10p_sei_hdr_h264
uint8_t itu_t_t35_hdr10p_sei_hdr_h264[NI_HDR10P_SEI_HDR_H264_LEN]
Definition: ni_device_api.h:1371
_ni_t408_config_t::numTicksPocDiffOne
uint32_t numTicksPocDiffOne
Definition: ni_device_api_priv.h:412
_ni_t408_config_t::enable_cu_level_rate_control
int32_t enable_cu_level_rate_control
Definition: ni_device_api_priv.h:401
_ni_dec_mastering_display_colour_volume_bytes
decoded payload format of HDR SEI mastering display colour volume
Definition: ni_device_api.h:1028
_ni_lat_meas_q_t
Definition: ni_lat_meas.h:39
_ni_decoder_input_params_t::ec_policy
int ec_policy
Definition: ni_device_api.h:2426
_ni_t408_config_t::customMDEnable
uint32_t customMDEnable
Definition: ni_device_api_priv.h:450
ni_device_api_priv.h
Private definitions used by ni_device_api.c for video processing tasks.
_ni_frame::metadata_buffer_size
uint32_t metadata_buffer_size
Definition: ni_device_api.h:2592
_ni_encoder_config_t::i8cuTreeFactor
int8_t i8cuTreeFactor
Definition: ni_device_api_priv.h:621
_ni_nvme_identity::xcoder_num_h265_encoder_hw
uint8_t xcoder_num_h265_encoder_hw
Definition: ni_nvme.h:163
_ni_decoder_input_params_t::skip_pts_guess
int skip_pts_guess
Definition: ni_device_api.h:2431
_ni_t408_config_t::dependSliceModeArg
int32_t dependSliceModeArg
Definition: ni_device_api_priv.h:383
_ni_t408_config_t::pu32IntraPlanarDeltaRate
int32_t pu32IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:464
_ni_encoder_cfg_params::temporal_layers_enable
int temporal_layers_enable
Definition: ni_device_api.h:2311
ni_clear_instance_buf
ni_retcode_t ni_clear_instance_buf(niFrameSurface1_t *surface)
clear a particular xcoder instance buffer/data
Definition: ni_device_api_priv.c:13212
NI_NUM_OF_PIXELS_1080P
#define NI_NUM_OF_PIXELS_1080P
Definition: ni_device_api.h:77
_ni_session_context::fn
unsigned short fn
Definition: ni_device_api.h:1573
_ni_packet::flags
int flags
Definition: ni_device_api.h:2745
NI_DEVICE_TYPE_DECODER
@ NI_DEVICE_TYPE_DECODER
Definition: ni_defs.h:341
_niFrameSurface1::ui16height
uint16_t ui16height
Definition: ni_device_api.h:2692
NI_GET_MAX_HWDESC_P2P_BUF_ID
#define NI_GET_MAX_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:266
_ni_session_context::enc_pts_w_idx
int64_t enc_pts_w_idx
Definition: ni_device_api.h:1392
_ni_session_context::key_frame_type
int key_frame_type
Definition: ni_device_api.h:1476
_ni_encoder_config_t::ui32ltrNextInterval
uint32_t ui32ltrNextInterval
Definition: ni_device_api_priv.h:566
READ_INSTANCE_R
#define READ_INSTANCE_R(sid, instance)
Definition: ni_nvme.h:679
_ni_session_context::input_frame_fifo
ni_input_frame input_frame_fifo[120]
encoder:calculate PSNR start
Definition: ni_device_api.h:1660
_ni_encoder_config_t::ui8hrdEnable
uint8_t ui8hrdEnable
Definition: ni_device_api_priv.h:549
_ni_session_context::current_frame_delay
int current_frame_delay
Definition: ni_device_api.h:1644
_ni_encoder_config_t::i8crfMax
int8_t i8crfMax
Definition: ni_device_api_priv.h:616
ni_instance_mgr_general_status_t
struct _ni_instance_mgr_general_status ni_instance_mgr_general_status_t
_ni_session_context::session_id
uint32_t session_id
Definition: ni_device_api.h:1431
_ni_nvme_identity::hw0_codec_format
uint8_t hw0_codec_format
Definition: ni_nvme.h:169
ni_scaler_session_read_hwdesc
ni_retcode_t ni_scaler_session_read_hwdesc(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
read a hardware descriptor from a scaler session
Definition: ni_device_api_priv.c:14733
_ni_t408_config_t::enable_hvs_qp
int32_t enable_hvs_qp
Definition: ni_device_api_priv.h:402
_ni_t408_config_t::avcIdrPeriod
int32_t avcIdrPeriod
Definition: ni_device_api_priv.h:485
_ni_xcoder_params::source_width
int source_width
Definition: ni_device_api.h:2618
_ni_packet::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2726
NI_PARAM_AV1_MAX_WIDTH
#define NI_PARAM_AV1_MAX_WIDTH
Definition: ni_device_api.h:145
_ni_nvme_identity::hw0_video_profile
uint8_t hw0_video_profile
Definition: ni_nvme.h:175
_ni_frame::reconf_len
unsigned int reconf_len
Definition: ni_device_api.h:2551
NI_MAX_PACKET_SZ
#define NI_MAX_PACKET_SZ
Definition: ni_defs.h:222
_ni_t408_config_t
Definition: ni_device_api_priv.h:364
_ni_input_frame::usable
int8_t usable
Definition: ni_device_api.h:1356
_ni_decoder_input_params_t::enable_ppu_scale_limit
int enable_ppu_scale_limit
Definition: ni_device_api.h:2429
_ni_session_config_rw::ui16WriteFrameId
uint16_t ui16WriteFrameId
Definition: ni_device_api_priv.h:75
_ni_encoder_cfg_params::ltrRefInterval
int ltrRefInterval
Definition: ni_device_api.h:2213
_ni_encoder_config_t::i8enableipRatio
int8_t i8enableipRatio
Definition: ni_device_api_priv.h:597
ni_query_vf_ns_id
ni_retcode_t ni_query_vf_ns_id(ni_device_handle_t device_handle, ni_device_vf_ns_id_t *p_dev_ns_vf, uint8_t fw_rev[])
Query VF and NS id from device.
Definition: ni_device_api.c:11052
_ni_nvme_identity::hw0_id
uint8_t hw0_id
Definition: ni_nvme.h:166
_ni_device_capability::xcoder_devices
ni_hw_capability_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
Definition: ni_device_api.h:1114
_ni_nvme_identity::hw1_video_profile
uint8_t hw1_video_profile
Definition: ni_nvme.h:188
ni_config_instance_network_binary
ni_retcode_t ni_config_instance_network_binary(ni_session_context_t *p_ctx, void *nb_data, uint32_t nb_size)
Definition: ni_device_api_priv.c:14929
_ni_device_capability::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_device_api.h:1120
NI_HDR10P_SEI_HDR_H264_LEN
#define NI_HDR10P_SEI_HDR_H264_LEN
Definition: ni_device_api.h:450
_ni_session_context::netint_fd
int netint_fd
Definition: ni_device_api.h:1569
_ni_hw_capability::hw_id
uint8_t hw_id
Definition: ni_device_api.h:1091
_ni_frame_config::frame_index
uint16_t frame_index
Definition: ni_device_api.h:2713
_ni_session_context::keyframe_factor
int keyframe_factor
Definition: ni_device_api.h:1481
_ni_encoder_config_t::ui8crfMaxIframeEnable
uint8_t ui8crfMaxIframeEnable
Definition: ni_device_api_priv.h:633
OPT_2
@ OPT_2
Definition: ni_device_api_priv.c:95
NI_CODEC_HW_NONE
@ NI_CODEC_HW_NONE
Definition: ni_device_api.h:906
NI_PIX_FMT_YUV420P
@ NI_PIX_FMT_YUV420P
Definition: ni_device_api.h:262
_ni_uploader_config_t
Definition: ni_device_api_priv.h:641
_ni_encoder_config_t::ui16aspectRatioWidth
uint16_t ui16aspectRatioWidth
Definition: ni_device_api_priv.h:554
_ni_resolution
Definition: ni_device_api_priv.h:654
_ni_encoder_config_t::i32maxIntraSize
int32_t i32maxIntraSize
Definition: ni_device_api_priv.h:514
ni_device_type_t
ni_device_type_t
Definition: ni_defs.h:336
_niFrameSurface1::ui32nodeAddress
uint32_t ui32nodeAddress
Definition: ni_device_api.h:2693
_ni_session_context::isP2P
int32_t isP2P
Definition: ni_device_api.h:1568
_ni_packet::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2725
_ni_encoder_cfg_params::enable_hvs_qp_scale
int enable_hvs_qp_scale
Definition: ni_device_api.h:2300
ni_frame_buffer_alloc
ni_retcode_t ni_frame_buffer_alloc(ni_frame_t *p_frame, int video_width, int video_height, int alignment, int metadata_flag, int factor, int hw_frame_count, int is_planar)
Allocate preliminary memory for the frame buffer based on provided parameters. Applicable to YUV420 P...
Definition: ni_device_api.c:2053
_ni_thread_arg_struct_t::close_thread
bool close_thread
Definition: ni_device_api.h:1213
_ni_session_context::p_leftover
uint8_t * p_leftover
Definition: ni_device_api.h:1457
_ni_network_buffer::ui16Height
uint16_t ui16Height
Definition: ni_device_api_priv.h:736
_ni_metadata_enc_bstream_rev61::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:266
_ni_t408_config_t::useLongTerm
uint32_t useLongTerm
Definition: ni_device_api_priv.h:436
NI_MAX_RESOLUTION_RGBA_WIDTH
#define NI_MAX_RESOLUTION_RGBA_WIDTH
Definition: ni_device_api.h:103
ni_decoder_output_config_t::sOutputPictureSize
ni_decoder_output_picture_size sOutputPictureSize
Definition: ni_device_api_priv.h:702
_ni_xcoder_params::enable2PassGop
int enable2PassGop
Definition: ni_device_api.h:2677
_ni_t408_config_t::roiEnable
int32_t roiEnable
Definition: ni_device_api_priv.h:408
_ni_device_capability::model_number
uint8_t model_number[40]
Definition: ni_device_api.h:1117
_ni_t408_config_t::cu16InterDeltaRate
int32_t cu16InterDeltaRate
Definition: ni_device_api_priv.h:471
_ni_packet::dts
long long dts
Definition: ni_device_api.h:2721
_ni_encoder_cfg_params::crf_max_iframe_enable
int crf_max_iframe_enable
Definition: ni_device_api.h:2342
WRITE_INSTANCE_W
#define WRITE_INSTANCE_W(sid, instance)
Definition: ni_nvme.h:673
ni_set_custom_template
void ni_set_custom_template(ni_session_context_t *p_ctx, ni_encoder_config_t *p_cfg, ni_xcoder_params_t *p_src)
Setup all xcoder configurations with custom parameters (Rev. B)
Definition: ni_device_api_priv.c:8186
NI_RETCODE_SUCCESS
@ NI_RETCODE_SUCCESS
Definition: ni_defs.h:420
_ni_session_context::active_video_width
uint32_t active_video_width
Definition: ni_device_api.h:1500
_ni_t408_config_t::gop_preset_index
int32_t gop_preset_index
Definition: ni_device_api_priv.h:372
_ni_t408_config_t::intraRefreshMode
int32_t intraRefreshMode
Definition: ni_device_api_priv.h:384
_ni_nvme_identity::ai8Mn
uint8_t ai8Mn[40]
Definition: ni_nvme.h:96
_ni_network_layer_params_t::num_of_dims
uint32_t num_of_dims
Definition: ni_device_api.h:1279
_ni_nvme_identity_xcoder_hw::hw_max_number_of_contexts
uint8_t hw_max_number_of_contexts
Definition: ni_nvme.h:75
_ni_network_layer_params_t::data_format
int32_t data_format
Definition: ni_device_api.h:1282
_ni_nvme_identity::hw3_codec_type
uint8_t hw3_codec_type
Definition: ni_nvme.h:209
_ni_metadata_common::crop_left
uint16_t crop_left
Definition: ni_device_api_priv.h:209
_ni_uploader_config_t::ui8Pool
uint8_t ui8Pool
Definition: ni_device_api_priv.h:647
_ni_gop_params::poc_offset
int poc_offset
Definition: ni_device_api.h:1903
_ni_gop_params::qp_offset
int qp_offset
Definition: ni_device_api.h:1904
_ni_framerate::framerate_denom
int32_t framerate_denom
Definition: ni_device_api.h:668
ni_resolution_t
struct _ni_resolution ni_resolution_t
NI_RETCODE_PARAM_ERROR_PIC_WIDTH
@ NI_RETCODE_PARAM_ERROR_PIC_WIDTH
Definition: ni_defs.h:473
NI_DEVICE_TYPE_UPLOAD
@ NI_DEVICE_TYPE_UPLOAD
Definition: ni_defs.h:348
_ni_scaler_config::numInputs
uint8_t numInputs
Definition: ni_device_api_priv.h:746
QUERY_INSTANCE_AI_INFO_R
#define QUERY_INSTANCE_AI_INFO_R(sid, instance)
Definition: ni_nvme.h:713
NI_CC_SEI_BYTE1
#define NI_CC_SEI_BYTE1
Definition: ni_device_api.h:432
_ni_t408_config_t::hvs_qp_scale
int32_t hvs_qp_scale
Definition: ni_device_api_priv.h:403
_ni_session_context::pts_correction_last_dts
int64_t pts_correction_last_dts
Definition: ni_device_api.h:1394
ni_ai_session_open
ni_retcode_t ni_ai_session_open(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:15811
_ni_session_context::status
int status
Definition: ni_device_api.h:1475
_ni_decoder_config_t::ui8EcPolicy
uint8_t ui8EcPolicy
Definition: ni_device_api_priv.h:718
_ni_session_context::domain
unsigned short domain
Definition: ni_device_api.h:1570
_ni_session_context::is_dec_pkt_512_aligned
int is_dec_pkt_512_aligned
Definition: ni_device_api.h:1410
_ni_session_context::pool_type
ni_frame_pool_type_t pool_type
Definition: ni_device_api.h:1648
_ni_t408_config_t::nrNoiseSigmaY
uint32_t nrNoiseSigmaY
Definition: ni_device_api_priv.h:432
NI_INVALID_SESSION_ID
#define NI_INVALID_SESSION_ID
Definition: ni_device_api.h:111
_ni_t408_config_t::maxQpI
int32_t maxQpI
Definition: ni_device_api_priv.h:478
_ni_t408_config_t::level
int32_t level
Definition: ni_device_api_priv.h:367
_ni_encoder_cfg_params::enable_ssim
int enable_ssim
Definition: ni_device_api.h:2306
ni_network_layer_params_t
struct _ni_network_layer_params_t ni_network_layer_params_t
_ni_session_context::param_err_msg
char param_err_msg[512]
Definition: ni_device_api.h:1479
_ni_device_capability::xcoder_cnt
uint8_t xcoder_cnt[NI_DEVICE_TYPE_XCODER_MAX]
Definition: ni_device_api.h:1113
_ni_session_context::ori_chroma_linesize
int ori_chroma_linesize
Definition: ni_device_api.h:1632
_ni_instance_mgr_general_status::active_sub_instances_cnt_overall
uint8_t active_sub_instances_cnt_overall
Definition: ni_device_api_priv.h:101
_ni_network_layer_offset
Definition: ni_device_api.h:1309
_ni_frame::total_start_len
uint32_t total_start_len
Definition: ni_device_api.h:2601
_ni_session_context::last_bitrate
int32_t last_bitrate
Definition: ni_device_api.h:1625
_ni_input_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:1351
_ni_session_context::pts_correction_num_faulty_dts
int pts_correction_num_faulty_dts
Definition: ni_device_api.h:1393
ni_xcoder_session_query_detail
int ni_xcoder_session_query_detail(ni_session_context_t *p_ctx, ni_device_type_t device_type, void *detail_data, int ver)
Query current xcoder status.
Definition: ni_device_api_priv.c:3083
GC620_ARGB8888
#define GC620_ARGB8888
Definition: ni_device_api.h:203
_ni_session_context::actual_video_width
uint32_t actual_video_width
Definition: ni_device_api.h:1504
ni_config_instance_set_sequence_change
ni_retcode_t ni_config_instance_set_sequence_change(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_resolution_t *p_resolution)
Send a p_config command to inform encoder sequence change.
Definition: ni_device_api_priv.c:6908
ni_gettime_ns
uint64_t ni_gettime_ns(void)
Definition: ni_util.c:1980
_ni_encoder_config_t::ui8compressor
uint8_t ui8compressor
Definition: ni_device_api_priv.h:600
_ni_encoder_cfg_params::chromaQpOffset
int chromaQpOffset
Definition: ni_device_api.h:2253
ni_send_session_keep_alive
ni_retcode_t ni_send_session_keep_alive(uint32_t session_id, ni_device_handle_t device_handle, ni_event_handle_t event_handle, void *p_data)
send a keep alive message to firmware
Definition: ni_device_api_priv.c:1391
_ni_encoder_config_t::ui8colorPrimaries
uint8_t ui8colorPrimaries
Definition: ni_device_api_priv.h:551
NI_PIX_FMT_BGR0
@ NI_PIX_FMT_BGR0
Definition: ni_device_api.h:270
_ni_t408_config_t::fixedBitRatio
int32_t fixedBitRatio[NI_MAX_GOP_NUM]
Definition: ni_device_api_priv.h:400
_ni_session_config_rw
Definition: ni_device_api_priv.h:68
_ni_encoder_config_t::i32qcomp
int32_t i32qcomp
Definition: ni_device_api_priv.h:617
_ni_instance_mgr_general_status::process_load_percent
uint8_t process_load_percent
Definition: ni_device_api_priv.h:82
_ni_frameclone_desc
Definition: ni_device_api.h:1326
NI_MIN_RESOLUTION_HEIGHT_SCALER
#define NI_MIN_RESOLUTION_HEIGHT_SCALER
Definition: ni_device_api.h:95
ni_query_eos
int ni_query_eos(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_instance_mgr_stream_complete_t *p_stream_complete)
Query a particular xcoder instance to get End of Output data.
Definition: ni_device_api_priv.c:6172
_ni_xcoder_params::rootBufId
int rootBufId
Definition: ni_device_api.h:2670
_ni_encoder_config_t::ui32ltrRefInterval
uint32_t ui32ltrRefInterval
Definition: ni_device_api_priv.h:563
OPT_3
@ OPT_3
Definition: ni_device_api_priv.c:96
_ni_t408_config_t::pu04DeltaRate
int32_t pu04DeltaRate
Definition: ni_device_api_priv.h:451
_ni_ddr_priority_config::ddr_mode
uint8_t ddr_mode
Definition: ni_device_api_priv.h:753
_ni_frame::p_start_buffer
uint8_t * p_start_buffer
Definition: ni_device_api.h:2598
_ni_session_context::event_handle
ni_event_handle_t event_handle
Definition: ni_device_api.h:1525
_ni_encoder_config_t::i16bitrateWindow
int16_t i16bitrateWindow
Definition: ni_device_api_priv.h:574
QUERY_INSTANCE_WBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_WBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:733
QUERY_INSTANCE_ACQUIRE_BUF
#define QUERY_INSTANCE_ACQUIRE_BUF(sid, instance)
Definition: ni_nvme.h:740
NI_XCODER_FAILURES_MAX
#define NI_XCODER_FAILURES_MAX
Definition: ni_device_api_priv.c:168
ni_set_custom_dec_template
void ni_set_custom_dec_template(ni_session_context_t *p_ctx, ni_decoder_config_t *p_cfg, ni_xcoder_params_t *p_src, uint32_t max_pkt_size)
Setup all xcoder configurations with custom parameters (Rev. B)
Definition: ni_device_api_priv.c:7973
NI_XCODER_REVISION
#define NI_XCODER_REVISION
Definition: ni_defs.h:95
QUERY_DETAIL_GET_STATUS_R
#define QUERY_DETAIL_GET_STATUS_R(instance)
Definition: ni_nvme.h:784
_ni_t408_config_t::maxQpP
int32_t maxQpP
Definition: ni_device_api_priv.h:480
_ni_device_capability::hw_elements_cnt
uint8_t hw_elements_cnt
Definition: ni_device_api.h:1111
_ni_nvme_identity::xcoder_num_h265_decoder_hw
uint8_t xcoder_num_h265_decoder_hw
Definition: ni_nvme.h:162
ni_config_instance_set_scaler_params
ni_retcode_t ni_config_instance_set_scaler_params(ni_session_context_t *p_ctx, ni_scaler_params_t *p_params)
Send a p_config command to configure scaling parameters.
Definition: ni_device_api_priv.c:5213
ni_instance_mgr_detail_status_t
struct _ni_instance_mgr_detail_status ni_instance_mgr_detail_status_t
_ni_frame::crop_top
uint32_t crop_top
Definition: ni_device_api.h:2509
ni_check_common_params
ni_retcode_t ni_check_common_params(ni_t408_config_t *p_param, ni_xcoder_params_t *p_src, char *p_param_err, uint32_t max_err_len)
Definition: ni_device_api_priv.c:11865
_ni_encoder_cfg_params::enable_acq_limit
int enable_acq_limit
Definition: ni_device_api.h:2348
_ni_encoder_cfg_params::noMbtree
int noMbtree
Definition: ni_device_api.h:2320
_ni_t408_config_t::nrNoiseSigmaCb
uint32_t nrNoiseSigmaCb
Definition: ni_device_api_priv.h:433
ni_scaler_multi_config_frame
ni_retcode_t ni_scaler_multi_config_frame(ni_session_context_t *p_ctx, ni_frame_config_t p_cfg_in[], int numInCfgs, ni_frame_config_t *p_cfg_out)
config multiple frames in the scaler
Definition: ni_device_api_priv.c:5633
_ni_session_context::last_dts
int64_t last_dts
Definition: ni_device_api.h:1389
_ni_session_context::blk_io_handle
ni_device_handle_t blk_io_handle
Definition: ni_device_api.h:1416
ni_config_read_inout_layers
ni_retcode_t ni_config_read_inout_layers(ni_session_context_t *p_ctx, ni_network_data_t *p_network)
Definition: ni_device_api_priv.c:15538
ni_timestamp_get_with_threshold
ni_retcode_t ni_timestamp_get_with_threshold(ni_timestamp_table_t *p_table, uint64_t frame_info, int64_t *p_timestamp, int32_t threshold, int32_t print, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1405
_ni_instance_mgr_general_status::process_load_percent_upper
uint8_t process_load_percent_upper
Definition: ni_device_api_priv.h:99
NI_XCODER_QUADRA
@ NI_XCODER_QUADRA
Definition: ni_defs.h:328
ni_lat_meas_q_add_entry
void * ni_lat_meas_q_add_entry(ni_lat_meas_q_t *frame_time_q, uint64_t abs_time, int64_t ts_time)
Add a new entry to latency queue.
Definition: ni_lat_meas.c:181
_ni_frame::start_len
uint32_t start_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2600
OPEN_SESSION_CODEC
#define OPEN_SESSION_CODEC(instance, codec, param)
Definition: ni_nvme.h:693
NI_HDR10P_SEI_HDR_HEVC_LEN
#define NI_HDR10P_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:449
NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_AREA_TOO_BIG
Definition: ni_defs.h:495
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_32X32_EN
Definition: ni_defs.h:481
_ni_encoder_cfg_params::roi_enable
int roi_enable
Definition: ni_device_api.h:2185
_ni_session_context::frame_pkt_offset
uint64_t frame_pkt_offset
Definition: ni_device_api.h:1598
NI_MAX_DEVICES_PER_HW_INSTANCE
#define NI_MAX_DEVICES_PER_HW_INSTANCE
Definition: ni_defs.h:234
_ni_encoder_cfg_params::intra_period
int intra_period
Definition: ni_device_api.h:2188
_ni_session_context::pts_correction_last_pts
int64_t pts_correction_last_pts
Definition: ni_device_api.h:1396
_ni_encoder_cfg_params::EnableRdoQuant
int EnableRdoQuant
Definition: ni_device_api.h:2230
_ni_frame_config::rectangle_height
uint16_t rectangle_height
Definition: ni_device_api.h:2709
_ni_decoder_input_params_t::min_packets_delay
bool min_packets_delay
Definition: ni_device_api.h:2434
_ni_nvme_identity::ui16Vid
uint16_t ui16Vid
Definition: ni_nvme.h:93
_ni_frame::separate_metadata
uint8_t separate_metadata
Definition: ni_device_api.h:2594
_ni_metadata_common::ui64_data
union _ni_metadata_common::@24 ui64_data
_ni_nvme_identity::hw3_video_profile
uint8_t hw3_video_profile
Definition: ni_nvme.h:214
NI_MAX_DEC_REJECT
#define NI_MAX_DEC_REJECT
Definition: ni_defs.h:284
_ni_encoder_cfg_params::enable_pic_skip
int enable_pic_skip
Definition: ni_device_api.h:2289
_ni_network_data::outset
ni_network_layer_offset_t * outset
Definition: ni_device_api.h:1322
_ni_network_perf_metrics
Definition: ni_device_api.h:1335
_ni_hw_capability::max_4k_fps
uint8_t max_4k_fps
Definition: ni_device_api.h:1093
_ni_t408_config_t::betaOffsetDiv2
int32_t betaOffsetDiv2
Definition: ni_device_api_priv.h:394
_ni_encoder_cfg_params::use_recommend_enc_params
int use_recommend_enc_params
Definition: ni_device_api.h:2265
_ni_thread_arg_struct_t::p_buffer
void * p_buffer
Definition: ni_device_api.h:1217
_ni_packet::frame_type
uint32_t frame_type
Definition: ni_device_api.h:2729
_ni_encoder_config_t::ui8disableBframeRDOQ
uint8_t ui8disableBframeRDOQ
Definition: ni_device_api_priv.h:635
_ni_encoder_config_t::ui8planarFormat
uint8_t ui8planarFormat
Definition: ni_device_api_priv.h:557
_ni_hw_capability::min_video_height
uint16_t min_video_height
Definition: ni_device_api.h:1099
_ni_load_query::fw_share_mem_usage
uint32_t fw_share_mem_usage
Definition: ni_device_api.h:1164
ni_query_session_statistic_info
ni_retcode_t ni_query_session_statistic_info(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_session_statistic_t *p_session_statistic)
Query a particular xcoder session to get session statistics.
Definition: ni_device_api_priv.c:6340
_ni_gop_params::pic_type
int pic_type
Definition: ni_device_api.h:1907
ni_metadata_enc_bstream_t
struct _ni_metadata_enc_bstream ni_metadata_enc_bstream_t
QUERY_INSTANCE_WBUFF_SIZE_R
#define QUERY_INSTANCE_WBUFF_SIZE_R(sid, instance)
Definition: ni_nvme.h:724
_ni_encoder_config_t::ui8LookAheadDepth
uint8_t ui8LookAheadDepth
Definition: ni_device_api_priv.h:536
_ni_encoder_cfg_params::min_qp
int min_qp
Definition: ni_device_api.h:2285
_ni_encoder_config_t::i32userMinDeltaQp
int32_t i32userMinDeltaQp
Definition: ni_device_api_priv.h:516
_ni_encoder_cfg_params::gop_preset_index
int gop_preset_index
Definition: ni_device_api.h:2179
_ni_instance_mgr_stream_info::pix_format
uint8_t pix_format
Definition: ni_device_api_priv.h:130
_ni_encoder_config_t::ui16hdr10_dy2
uint16_t ui16hdr10_dy2
Definition: ni_device_api_priv.h:590
_ni_frame::sei_user_data_unreg_len
unsigned int sei_user_data_unreg_len
Definition: ni_device_api.h:2540
_ni_encoder_cfg_params::max_qp
int max_qp
Definition: ni_device_api.h:2286
ni_scaler_alloc_frame
ni_retcode_t ni_scaler_alloc_frame(ni_session_context_t *p_ctx, int width, int height, int format, int options, int rectangle_width, int rectangle_height, int rectangle_x, int rectangle_y, int rgba_color, int frame_index)
allocate a frame in the scaler
Definition: ni_device_api_priv.c:5315
_ni_frame_config
Definition: ni_device_api.h:2702
_ni_encoder_cfg_params::ipRatio
float ipRatio
Definition: ni_device_api.h:2323
NI_SCALER_FLAG_IO
#define NI_SCALER_FLAG_IO
Definition: ni_device_api.h:282
ni_decoder_output_config_t::ui8Enabled
uint8_t ui8Enabled
Definition: ni_device_api_priv.h:689
_ni_encoder_config_t::ui8av1ErrResilientMode
uint8_t ui8av1ErrResilientMode
Definition: ni_device_api_priv.h:602
_ni_session_context::force_frame_type
int force_frame_type
Definition: ni_device_api.h:1493
_ni_encoder_cfg_params::blockRCSize
int blockRCSize
Definition: ni_device_api.h:2258
NI_SCALER_OPCODE_IPOVLY
@ NI_SCALER_OPCODE_IPOVLY
Definition: ni_defs.h:573
ni_pthread_cond_signal
int ni_pthread_cond_signal(ni_pthread_cond_t *cond)
signal a condition
Definition: ni_util.c:4237
_ni_encoder_config_t::i32meBlkMode
int32_t i32meBlkMode
Definition: ni_device_api_priv.h:508
_ni_instance_buf_info
Definition: ni_device_api_priv.h:187
_ni_t408_config_t::nrNoiseEstEnable
uint32_t nrNoiseEstEnable
Definition: ni_device_api_priv.h:431
_ni_lat_meas_q_t::last_benchmark_time
uint64_t last_benchmark_time
Definition: ni_lat_meas.h:42
_ni_session_statistic_t::ui32WrBufAvailSize
uint32_t ui32WrBufAvailSize
Definition: ni_defs.h:371
_ni_network_buffer::ui16FrameIdx
uint16_t ui16FrameIdx[4]
Definition: ni_device_api_priv.h:740
_ni_xcoder_params::roi_demo_mode
int roi_demo_mode
Definition: ni_device_api.h:2625
_ni_sei_header::status
uint8_t status
Definition: ni_device_api.h:332
_niFrameSurface1::src_cpu
int8_t src_cpu
Definition: ni_device_api.h:2698
_ni_network_perf_metrics::total_idle_cycles
uint32_t total_idle_cycles
Definition: ni_device_api.h:1338
_ni_metadata_enc_bstream::frameCropTopOffset
uint16_t frameCropTopOffset
Definition: ni_device_api_priv.h:301
_ni_session_context::src_endian
int src_endian
Definition: ni_device_api.h:1444
_ni_session_context::low_delay_sync_mutex
ni_pthread_mutex_t low_delay_sync_mutex
Definition: ni_device_api.h:1616
_ni_encoder_config_t::i32userQpMin
int32_t i32userQpMin
Definition: ni_device_api_priv.h:517
_ni_encoder_cfg_params::intra_mb_refresh_mode
int intra_mb_refresh_mode
Definition: ni_device_api.h:2189
nvme_config_xcoder_config_set_sequence_change
@ nvme_config_xcoder_config_set_sequence_change
Definition: ni_nvme.h:434
_ni_encoder_config_t::ui8lowLatencyMode
uint8_t ui8lowLatencyMode
Definition: ni_device_api_priv.h:523
g_device_in_ctxt
bool g_device_in_ctxt
Definition: ni_rsrc_api.cpp:62
NI_MIN_RESOLUTION_WIDTH_SCALER
#define NI_MIN_RESOLUTION_WIDTH_SCALER
Definition: ni_device_api.h:94
DOWNLOAD_FRAMEIDX_R
#define DOWNLOAD_FRAMEIDX_R(frame_id)
Definition: ni_nvme.h:682
_ni_frame::orignal_pts
long long orignal_pts
Definition: ni_device_api.h:2605
NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_TOP
Definition: ni_defs.h:458
_ni_encoder_cfg_params::vbv_buffer_size
int vbv_buffer_size
Definition: ni_device_api.h:2296
_ni_t408_config_t::nrIntraWeightCb
uint32_t nrIntraWeightCb
Definition: ni_device_api_priv.h:425
_ni_nvme_identity::device_is_xcoder
uint8_t device_is_xcoder
Definition: ni_nvme.h:155
ni_decoder_output_config_t::ui8ScaleResCeil
uint8_t ui8ScaleResCeil
Definition: ni_device_api_priv.h:700
_ni_frame::sei_total_len
unsigned int sei_total_len
Definition: ni_device_api.h:2525
_ni_t408_config_t::conf_win_right
int32_t conf_win_right
Definition: ni_device_api_priv.h:379
INST_BUF_INFO_RW_WRITE_BUSY
@ INST_BUF_INFO_RW_WRITE_BUSY
Definition: ni_device_api_priv.h:52
_ni_session_context::pts_offsets
int64_t pts_offsets[NI_FIFO_SZ]
Definition: ni_device_api.h:1400
_ni_frame::p_buffer
uint8_t * p_buffer
Definition: ni_device_api.h:2562
NUM_OF_CORES
@ NUM_OF_CORES
Definition: ni_defs.h:366
_ni_metadata_enc_bstream::max_mv_x
int16_t max_mv_x[2]
Definition: ni_device_api_priv.h:287
_ni_encoder_config_t::i32ipRatio
int32_t i32ipRatio
Definition: ni_device_api_priv.h:623
_ni_encoder_cfg_params::noHWMultiPassSupport
int noHWMultiPassSupport
Definition: ni_device_api.h:2321
_ni_session_statistic_t::ui32FramesCompleted
uint32_t ui32FramesCompleted
Definition: ni_defs.h:376
_ni_encoder_cfg_params::gdrDuration
int gdrDuration
Definition: ni_device_api.h:2212
NI_RETCODE_PARAM_ERROR_CONF_WIN_L
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_L
Definition: ni_defs.h:462
_ni_xcoder_params::video_full_range_flag
int video_full_range_flag
Definition: ni_device_api.h:2656
NI_FIFO_SZ
#define NI_FIFO_SZ
Definition: ni_defs.h:286
_ni_network_buffer::ui8PoolSize
uint8_t ui8PoolSize
Definition: ni_device_api_priv.h:738
_ni_frame::dts
long long dts
Definition: ni_device_api.h:2502
_ni_decoder_input_params_t::force_low_delay
bool force_low_delay
Definition: ni_device_api.h:2421
_ni_metadata_common::crop_right
uint16_t crop_right
Definition: ni_device_api_priv.h:210
_ni_encoder_cfg_params::aspectRatioWidth
int aspectRatioWidth
Definition: ni_device_api.h:2168
NI_RETCODE_INVALID_PARAM
@ NI_RETCODE_INVALID_PARAM
Definition: ni_defs.h:422
_ni_metadata_enc_bstream::reconLumaWidth
uint16_t reconLumaWidth
Definition: ni_device_api_priv.h:299
ni_decoder_session_write
int ni_decoder_session_write(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Send a video p_packet to decoder.
Definition: ni_device_api_priv.c:1698
ni_ai_session_write
ni_retcode_t ni_ai_session_write(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Definition: ni_device_api_priv.c:15251
_ni_frame::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2595
ni_buffer_pool_free
void ni_buffer_pool_free(ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:771
NI_AV1_INVALID_BUFFER_INDEX
#define NI_AV1_INVALID_BUFFER_INDEX
Definition: ni_device_api.h:179
_ni_encoder_config_t::i32picHeight
int32_t i32picHeight
Definition: ni_device_api_priv.h:507
_ni_device_capability::fw_commit_hash
uint8_t fw_commit_hash[41]
Definition: ni_device_api.h:1122
_ni_frame::crop_left
uint32_t crop_left
Definition: ni_device_api.h:2511
_ni_metadata_enc_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api_priv.h:257
_ni_nvme_identity::xcoder_num_devices
uint8_t xcoder_num_devices
Definition: ni_nvme.h:230
_ni_metadata_enc_bstream::ssimV
uint32_t ssimV
Definition: ni_device_api_priv.h:285
_ni_instance_mgr_stream_info
Definition: ni_device_api_priv.h:122
_ni_encoder_cfg_params::maxFrameSizeRatio
int maxFrameSizeRatio
Definition: ni_device_api.h:2172
_ni_decoder_input_params_t::enable_advanced_ec
int enable_advanced_ec
Definition: ni_device_api.h:2427
_ni_t408_config_t::bgThrMeanDiff
uint32_t bgThrMeanDiff
Definition: ni_device_api_priv.h:445
_ni_encoder_cfg_params::HDR10AveLight
int HDR10AveLight
Definition: ni_device_api.h:2199
_ni_encoder_config_t::ui32verOffset
uint32_t ui32verOffset
Definition: ni_device_api_priv.h:614
_ni_session_config_rw::uHWAccessField
union _ni_session_config_rw::@17 uHWAccessField
ni_decoder_session_read
int ni_decoder_session_read(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
Retrieve a YUV p_frame from decoder.
Definition: ni_device_api_priv.c:2209
_ni_decoder_config_t::ui8HWFrame
uint8_t ui8HWFrame
Definition: ni_device_api_priv.h:707
_ni_encoder_cfg_params::cu_size_mode
int cu_size_mode
Definition: ni_device_api.h:2267
ni_config_instance_set_write_len
ni_retcode_t ni_config_instance_set_write_len(ni_session_context_t *p_ctx, ni_device_type_t device_type, uint32_t len)
Send a p_config command to set the length for the incoming write packet.
Definition: ni_device_api_priv.c:6838
_ni_decoder_config_t::ui16MaxSeiDataSize
uint16_t ui16MaxSeiDataSize
Definition: ni_device_api_priv.h:709
ni_query_session_stats
ni_retcode_t ni_query_session_stats(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_session_stats_t *p_session_stats, int rc, int opcode)
Query a particular session to get the stats info.
Definition: ni_device_api_priv.c:5996
_ni_encoder_cfg_params::hrdEnable
int hrdEnable
Definition: ni_device_api.h:2231
_ni_encoder_config_t::i32bitRate
int32_t i32bitRate
Definition: ni_device_api_priv.h:518
_ni_t408_config_t::pu04IntraPlanarDeltaRate
int32_t pu04IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:455
SwapSWBytes
void SwapSWBytes(uint8_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:177
_ni_frame_config::session_id
uint16_t session_id
Definition: ni_device_api.h:2714
_ni_encoder_config_t::ui8AiEnhanceLevel
uint8_t ui8AiEnhanceLevel
Definition: ni_device_api_priv.h:615
_ni_t408_config_t::pu32IntraAngleDeltaRate
int32_t pu32IntraAngleDeltaRate
Definition: ni_device_api_priv.h:466
NI_PIX_FMT_8_TILED4X4
@ NI_PIX_FMT_8_TILED4X4
Definition: ni_device_api.h:275
_ni_session_context::bit_depth_factor
int bit_depth_factor
Definition: ni_device_api.h:1445
_ni_session_statistic_t::ui32FramesBuffered
uint32_t ui32FramesBuffered
Definition: ni_defs.h:375
_ni_uploader_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:650
_ni_encoder_config_t
Definition: ni_device_api_priv.h:503
ni_lat_meas_q_check_latency
uint64_t ni_lat_meas_q_check_latency(ni_lat_meas_q_t *frame_time_q, uint64_t abs_time, int64_t ts_time)
Check latency of a frame referenced by its timestamp.
Definition: ni_lat_meas.c:201
_ni_metadata_common::has_b_frame
uint8_t has_b_frame
Definition: ni_device_api_priv.h:221
_ni_encoder_config_t::ui32setLongTermInterval
uint32_t ui32setLongTermInterval
Definition: ni_device_api_priv.h:569
ni_network_layer_info_t
struct _ni_network_layer_info ni_network_layer_info_t
CONFIG_INSTANCE_SetAiFrm_W
#define CONFIG_INSTANCE_SetAiFrm_W(sid, instance)
Definition: ni_nvme.h:849
ni_decoder_output_config_t::ui8ScaleEnabled
uint8_t ui8ScaleEnabled
Definition: ni_device_api_priv.h:692
_ni_frame::force_pic_qp
uint16_t force_pic_qp
Definition: ni_device_api.h:2555
_ni_encoder_cfg_params::high_tier
int high_tier
Definition: ni_device_api.h:2263
_ni_session_context::mutex
ni_pthread_mutex_t mutex
Definition: ni_device_api.h:1516
_ni_encoder_cfg_params::max_num_merge
int max_num_merge
Definition: ni_device_api.h:2268
_ni_gop_params::num_ref_pics
int num_ref_pics
Definition: ni_device_api.h:1908
_ni_decoder_input_params_t::crop_mode
int crop_mode[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2409
ni_config_instance_set_uploader_params
ni_retcode_t ni_config_instance_set_uploader_params(ni_session_context_t *p_ctx, uint32_t pool_size, uint32_t pool)
Send a p_config command to configure uploading parameters.
Definition: ni_device_api_priv.c:14503
_ni_encoder_cfg_params::crf
int crf
Definition: ni_device_api.h:2197
_ni_session_context::hw_id
int hw_id
Definition: ni_device_api.h:1429
ni_dec_fme_buffer_pool_free
void ni_dec_fme_buffer_pool_free(ni_buf_pool_t *p_buffer_pool)
Definition: ni_util.c:717
_ni_encoder_config_t::i8pass1Qp
int8_t i8pass1Qp
Definition: ni_device_api_priv.h:629
_ni_hw_capability::video_level
uint8_t video_level
Definition: ni_device_api.h:1101
NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
#define NI_MAX_DEC_SESSION_READ_QUERY_EOS_RETRIES
Definition: ni_device_api_priv.h:791
_ni_network_layer_params_t
Definition: ni_device_api.h:1277
_ni_encoder_config_t::ui8fixedframerate
uint8_t ui8fixedframerate
Definition: ni_device_api_priv.h:601
NVME_CORE
@ NVME_CORE
Definition: ni_defs.h:361
_ni_encoder_config_t::ui8colorTrc
uint8_t ui8colorTrc
Definition: ni_device_api_priv.h:552
_ni_xcoder_params::fps_number
uint32_t fps_number
Definition: ni_device_api.h:2613
QUERY_INSTANCE_RBUFF_SIZE_R
#define QUERY_INSTANCE_RBUFF_SIZE_R(sid, instance)
Definition: ni_nvme.h:722
_ni_session_context::ddr_config
uint8_t ddr_config
Definition: ni_device_api.h:1577
_ni_t408_config_t::bgLambdaQp
uint32_t bgLambdaQp
Definition: ni_device_api_priv.h:446
_ni_nvme_identity::hw3_id
uint8_t hw3_id
Definition: ni_nvme.h:205
enqueue_ni_frame
void enqueue_ni_frame(ni_session_context_t *p_ctx, ni_frame_t *ni_frame)
Definition: ni_device_api_priv.c:3842
ni_cmp_fw_api_ver
int ni_cmp_fw_api_ver(const char ver1[], const char ver2[])
Compare two 3 character strings containing a FW API version. Handle comparision when FW API version f...
Definition: ni_util.c:3656
_ni_xcoder_params::hdrEnableVUI
int hdrEnableVUI
Definition: ni_device_api.h:2640
NI_HDR10P_SEI_BYTE5
#define NI_HDR10P_SEI_BYTE5
Definition: ni_device_api.h:444
_ni_load_query::fw_video_shared_mem_usage
uint32_t fw_video_shared_mem_usage
Definition: ni_device_api.h:1161
_ni_scaler_params_t
Definition: ni_device_api.h:2465
_ni_t408_config_t::conf_win_bottom
int32_t conf_win_bottom
Definition: ni_device_api_priv.h:377
_ni_nvme_identity::hw0_video_level
uint8_t hw0_video_level
Definition: ni_nvme.h:176
nvme_config_xcoder_config_set_write_legth
@ nvme_config_xcoder_config_set_write_legth
Definition: ni_nvme.h:432
NI_RETCODE_PARAM_ERROR_CU_LVL_RC_EN
@ NI_RETCODE_PARAM_ERROR_CU_LVL_RC_EN
Definition: ni_defs.h:448
_ni_t408_config_t::use_recommend_enc_params
int32_t use_recommend_enc_params
Definition: ni_device_api_priv.h:386
_ni_device_capability::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_device_api.h:1124
CONFIG_GLOBAL_NAMESPACE_NUM
#define CONFIG_GLOBAL_NAMESPACE_NUM
Definition: ni_nvme.h:885
_ni_p2p_sgl_t::ui64DMAAddr
uint64_t ui64DMAAddr[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:400
_ni_session_data_io::data
union _ni_session_data_io::@16 data
NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_BIG
Definition: ni_defs.h:491
_ni_timestamp_table_t::list
ni_queue_t list
Definition: ni_device_api.h:1274
_ni_metadata_enc_frame::start_len
uint16_t start_len[3]
Definition: ni_device_api_priv.h:256
NI_RETCODE_PARAM_ERROR_MX_QP
@ NI_RETCODE_PARAM_ERROR_MX_QP
Definition: ni_defs.h:454
STD_AV1
@ STD_AV1
Definition: ni_device_api_priv.h:359
NI_RETCODE_ERROR_MEM_ALOC
@ NI_RETCODE_ERROR_MEM_ALOC
Definition: ni_defs.h:424
ni_session_config_rw_type_t
ni_session_config_rw_type_t
Definition: ni_device_api_priv.h:40
ni_retcode_t
ni_retcode_t
Definition: ni_defs.h:418
_ni_metadata_enc_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api_priv.h:253
_ni_load_query::fw_model_load
uint32_t fw_model_load
Definition: ni_device_api.h:1153
_ni_metadata_enc_bstream::inter_total_count
uint16_t inter_total_count
Definition: ni_device_api_priv.h:292
_ni_t408_config_t::forcedHeaderEnable
uint32_t forcedHeaderEnable
Definition: ni_device_api_priv.h:500
_ni_instance_mgr_general_status::ui32UploadRate
uint32_t ui32UploadRate
Definition: ni_device_api_priv.h:109
_ni_nvme_identity::xcoder_num_h264_encoder_hw
uint8_t xcoder_num_h264_encoder_hw
Definition: ni_nvme.h:161
CONFIG_INSTANCE_Flush_W
#define CONFIG_INSTANCE_Flush_W(sid, instance)
Definition: ni_nvme.h:806
SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
@ SESSION_RUN_STATE_SEQ_CHANGE_DRAINING
Definition: ni_device_api.h:1133
NI_FW_META_DATA_SZ
#define NI_FW_META_DATA_SZ
Definition: ni_defs.h:301
NI_CC_SEI_HDR_H264_LEN
#define NI_CC_SEI_HDR_H264_LEN
Definition: ni_device_api.h:451
_ni_decoder_config_t::ui32SourceHeight
uint32_t ui32SourceHeight
Definition: ni_device_api_priv.h:723
ni_nvme_send_write_cmd
int32_t ni_nvme_send_write_cmd(ni_device_handle_t handle, ni_event_handle_t event_handle, void *p_data, uint32_t data_len, uint32_t lba)
Compose a io write command.
Definition: ni_nvme.c:629
_ni_session_context::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1576
nvme_cmd_xcoder_read
@ nvme_cmd_xcoder_read
Definition: ni_defs.h:560
_ni_network_data::output_num
uint32_t output_num
Definition: ni_device_api.h:1317
_ni_encoder_cfg_params::intra_mb_refresh_arg
int intra_mb_refresh_arg
Definition: ni_device_api.h:2190
XCODER_MAX_NUM_TEMPORAL_LAYER
#define XCODER_MAX_NUM_TEMPORAL_LAYER
Definition: ni_util.h:82
SESSION_RUN_STATE_NORMAL
@ SESSION_RUN_STATE_NORMAL
Definition: ni_device_api.h:1132
_ni_metadata_enc_frame::force_pic_qp_i
uint16_t force_pic_qp_i
Definition: ni_device_api_priv.h:249
nvme_cmd_xcoder_write
@ nvme_cmd_xcoder_write
Definition: ni_defs.h:559
ni_packet_buffer_alloc
ni_retcode_t ni_packet_buffer_alloc(ni_packet_t *p_packet, int packet_size)
Allocate memory for the packet buffer based on provided packet size.
Definition: ni_device_api.c:3643
NI_MAX_REF_PIC
#define NI_MAX_REF_PIC
Definition: ni_device_api.h:59
_ni_session_context::pts_correction_num_faulty_pts
int pts_correction_num_faulty_pts
Definition: ni_device_api.h:1395
_ni_encoder_cfg_params::pps_init_qp
int pps_init_qp
Definition: ni_device_api.h:2326
_ni_session_context::pkt_pos
uint64_t pkt_pos[NI_FIFO_SZ]
Definition: ni_device_api.h:1404
ni_ai_session_query_metrics
ni_retcode_t ni_ai_session_query_metrics(ni_session_context_t *p_ctx, ni_network_perf_metrics_t *p_metrics)
Definition: ni_device_api_priv.c:16879
NI_RETCODE_PARAM_INVALID_VALUE
@ NI_RETCODE_PARAM_INVALID_VALUE
Definition: ni_defs.h:430
MAGIC_P2P_VALUE
#define MAGIC_P2P_VALUE
Definition: ni_device_api_priv.c:84
ni_instance_buf_info_t
struct _ni_instance_buf_info ni_instance_buf_info_t
_ni_frame::sei_hdr_mastering_display_color_vol_offset
unsigned int sei_hdr_mastering_display_color_vol_offset
Definition: ni_device_api.h:2531
_ni_frame::sei_cc_offset
unsigned int sei_cc_offset
Definition: ni_device_api.h:2528
_ni_packet::pkt_pos
uint64_t pkt_pos
Definition: ni_device_api.h:2724
_ni_encoder_cfg_params::gopSize
int gopSize
Definition: ni_device_api.h:2251
ni_session_stats_t
struct _ni_session_stats ni_session_stats_t
_ni_session_context::average_psnr
double average_psnr
Definition: ni_device_api.h:1664
ni_decode_cropping_rectangle::ui16X
uint16_t ui16X
Definition: ni_device_api_priv.h:677
_ni_frame_config::options
uint16_t options
Definition: ni_device_api.h:2707
_ni_session_context::framerate
ni_framerate_t framerate
Definition: ni_device_api.h:1555
_ni_nvme_identity::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_nvme.h:221
ni_decoder_session_flush
ni_retcode_t ni_decoder_session_flush(ni_session_context_t *p_ctx)
Flush decoder output.
Definition: ni_device_api_priv.c:1488
_ni_session_context::pext_mutex
ni_pthread_mutex_t * pext_mutex
Definition: ni_device_api.h:1621
_ni_frame_config::picture_format
uint16_t picture_format
Definition: ni_device_api.h:2706
_ni_p2p_sgl_t::ui32NumEntries
uint32_t ui32NumEntries
Definition: ni_defs.h:398
_ni_thread_arg_struct_t::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1215
_ni_decoder_input_params_t::crop_whxy
int crop_whxy[NI_MAX_NUM_OF_DECODER_OUTPUTS][4]
Definition: ni_device_api.h:2410
NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
@ NI_RETCODE_PARAM_ERROR_VBV_BUFFER_SIZE
Definition: ni_defs.h:437
_ni_device_vf_ns_id::ns_id
uint16_t ns_id
Definition: ni_rsrc_api.h:231
_ni_input_frame::pts
int64_t pts
Definition: ni_device_api.h:1354
SwapSW16
void SwapSW16(uint16_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:188
_ni_encoder_cfg_params::qlevel
int qlevel
Definition: ni_device_api.h:2246
_ni_encoder_config_t::ui8rcEnable
uint8_t ui8rcEnable
Definition: ni_device_api_priv.h:520
NI_MAX_CONTEXTS_PER_HW_INSTANCE
#define NI_MAX_CONTEXTS_PER_HW_INSTANCE
Definition: ni_defs.h:231
_ni_session_statistic_t::ui32FramesInput
uint32_t ui32FramesInput
Definition: ni_defs.h:374
_ni_custom_gop_params::custom_gop_size
int custom_gop_size
Definition: ni_device_api.h:1994
XCODER_TEST_RECONF_OFF
@ XCODER_TEST_RECONF_OFF
Definition: ni_device_api.h:1683
_ni_session_statistic_t::ui32LastTransactionCompletionStatus
uint32_t ui32LastTransactionCompletionStatus
Definition: ni_defs.h:384
_ni_t408_config_t::internalBitDepth
int32_t internalBitDepth
Definition: ni_device_api_priv.h:369
_ni_t408_config_t::conf_win_top
int32_t conf_win_top
Definition: ni_device_api_priv.h:376
NI_PARAM_MAX_HEIGHT
#define NI_PARAM_MAX_HEIGHT
Definition: ni_device_api.h:137
_niFrameSurface1::ui16FrameIdx
uint16_t ui16FrameIdx
Definition: ni_device_api.h:2689
_ni_encoder_config_t::i8chromaQpOffset
int8_t i8chromaQpOffset
Definition: ni_device_api_priv.h:571
ni_encoder_session_open
ni_retcode_t ni_encoder_session_open(ni_session_context_t *p_ctx)
Open a xcoder encoder instance.
Definition: ni_device_api_priv.c:3116
_ni_encoder_cfg_params::HDR10dx2
int HDR10dx2
Definition: ni_device_api.h:2206
_ni_session_statistic_t::ui32LastErrorStatus
uint32_t ui32LastErrorStatus
Definition: ni_defs.h:386
_ni_encoder_config_t::i32cplxDecay
int32_t i32cplxDecay
Definition: ni_device_api_priv.h:625
_ni_metadata_enc_frame
Definition: ni_device_api_priv.h:239
ni_decoder_output_config_t::ui8EnablePpuScaleLimit
uint8_t ui8EnablePpuScaleLimit
Definition: ni_device_api_priv.h:699
_ni_metadata_enc_frame::force_pic_qp_p
uint16_t force_pic_qp_p
Definition: ni_device_api_priv.h:250
_ni_instance_mgr_stream_info::transfer_frame_height
uint16_t transfer_frame_height
Definition: ni_device_api_priv.h:127
_ni_session_context::psnr_v
double psnr_v
Definition: ni_device_api.h:1663
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:61
_ni_encoder_config_t::i8intraQpDelta
int8_t i8intraQpDelta
Definition: ni_device_api_priv.h:560
ni_device_capability_query
ni_retcode_t ni_device_capability_query(ni_device_handle_t device_handle, ni_device_capability_t *p_cap)
Query device and return device capability structure.
Definition: ni_device_api.c:584
_ni_frameclone_desc::ui16DstIdx
uint16_t ui16DstIdx
Definition: ni_device_api.h:1329
_ni_uploader_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:646
ni_hwframe_clone
ni_retcode_t ni_hwframe_clone(ni_session_context_t *p_ctx, ni_frameclone_desc_t *p_frameclone_desc)
Copy a src hw frame to a dst hw frame.
Definition: ni_device_api_priv.c:14412
NI_AI_FLAG_IO
#define NI_AI_FLAG_IO
Definition: ni_device_api.h:291
ni_timestamp_scan_cleanup
void ni_timestamp_scan_cleanup(ni_timestamp_table_t *pts_list, ni_timestamp_table_t *dts_list, ni_queue_buffer_pool_t *p_buffer_pool)
Definition: ni_util.c:1413
CONFIG_SESSION_Read_W
#define CONFIG_SESSION_Read_W(sid)
Definition: ni_nvme.h:864
DP_LOG_OFFSET_IN_4K
#define DP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:662
_ni_metadata_dec_frame
Definition: ni_device_api_priv.h:226
_ni_metadata_enc_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api_priv.h:254
_ni_nvme_identity
Definition: ni_nvme.h:89
CONFIG_INSTANCE_SetPktSize_W
#define CONFIG_INSTANCE_SetPktSize_W(sid, instance)
Definition: ni_nvme.h:840
_ni_encoder_cfg_params::HDR10wx
int HDR10wx
Definition: ni_device_api.h:2208
_ni_xcoder_params::source_height
int source_height
Definition: ni_device_api.h:2623
_ni_metadata_common::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:216
CONFIG_INSTANCE_SetSeqChange_W
#define CONFIG_INSTANCE_SetSeqChange_W(sid, instance)
Definition: ni_nvme.h:842
_ni_encoder_config_t::i32srcBitDepth
int32_t i32srcBitDepth
Definition: ni_device_api_priv.h:521
_ni_encoder_cfg_params::rcQpDeltaRange
int rcQpDeltaRange
Definition: ni_device_api.h:2259
_ni_nvme_identity::hw1_codec_format
uint8_t hw1_codec_format
Definition: ni_nvme.h:182
ni_decoder_frame_buffer_alloc
ni_retcode_t ni_decoder_frame_buffer_alloc(ni_buf_pool_t *p_pool, ni_frame_t *p_frame, int alloc_mem, int video_width, int video_height, int alignment, int factor, int is_planar)
Allocate memory for decoder frame buffer based on provided parameters; the memory is retrieved from a...
Definition: ni_device_api.c:2516
_ni_metadata_enc_bstream::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api_priv.h:279
_ni_encoder_cfg_params::hvsBaseMbComplexity
int hvsBaseMbComplexity
Definition: ni_device_api.h:2330
_ni_metadata_enc_bstream::ssimU
uint32_t ssimU
Definition: ni_device_api_priv.h:284
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:60
_ni_encoder_config_t::i8statisticOutputLevel
int8_t i8statisticOutputLevel
Definition: ni_device_api_priv.h:632
_ni_metadata_enc_frame::enc_reconfig_data_size
uint32_t enc_reconfig_data_size
Definition: ni_device_api_priv.h:245
IS_XCODER_DEVICE_TYPE
#define IS_XCODER_DEVICE_TYPE(t)
Definition: ni_defs.h:403
_ni_encoder_config_t::ui8setLongTermCount
uint8_t ui8setLongTermCount
Definition: ni_device_api_priv.h:581
_ni_encoder_config_t::i32tolCtbRcIntra
int32_t i32tolCtbRcIntra
Definition: ni_device_api_priv.h:573
_ni_session_context::src_bit_depth
int src_bit_depth
Definition: ni_device_api.h:1443
_ni_frame::ni_pict_type
ni_pic_type_t ni_pict_type
Definition: ni_device_api.h:2523
_ni_encoder_config_t::i32bitRateBL
int32_t i32bitRateBL
Definition: ni_device_api_priv.h:519
_ni_nvme_identity::hw2_id
uint8_t hw2_id
Definition: ni_nvme.h:192
_ni_session_stats
Definition: ni_device_api_priv.h:135
NI_MAX_RESOLUTION_WIDTH
#define NI_MAX_RESOLUTION_WIDTH
Definition: ni_device_api.h:100
_ni_framerate::framerate_num
int32_t framerate_num
Definition: ni_device_api.h:665
DP_CORE
@ DP_CORE
Definition: ni_defs.h:363
ni_scaler_session_open
int ni_scaler_session_open(ni_session_context_t *p_ctx)
Open a xcoder scaler instance.
Definition: ni_device_api_priv.c:4915
_ni_instance_mgr_general_status::process_load_percent_overall
uint8_t process_load_percent_overall
Definition: ni_device_api_priv.h:100
_ni_encoder_config_t::i8skipFrameEnable
int8_t i8skipFrameEnable
Definition: ni_device_api_priv.h:595
GOP_PRESET_IDX_DEFAULT
@ GOP_PRESET_IDX_DEFAULT
Definition: ni_device_api_priv.h:314
nvme_admin_cmd_xcoder_open
@ nvme_admin_cmd_xcoder_open
Definition: ni_nvme.h:318
NI_QUADRA_MEMORY_CONFIG_SR_4G
#define NI_QUADRA_MEMORY_CONFIG_SR_4G
Definition: ni_device_api_priv.h:784
_ni_encoder_cfg_params::HDR10maxluma
int HDR10maxluma
Definition: ni_device_api.h:2210
_ni_network_data::linfo
ni_network_layer_info_t linfo
Definition: ni_device_api.h:1318
_niFrameSurface1::ui16session_ID
uint16_t ui16session_ID
Definition: ni_device_api.h:2690
NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
@ NI_RETCODE_ERROR_UNSUPPORTED_FW_VERSION
Definition: ni_defs.h:514
_ni_instance_buf_info::buf_avail_size
uint32_t buf_avail_size
Definition: ni_device_api_priv.h:191
_ni_xcoder_params::cfg_enc_params
ni_encoder_cfg_params_t cfg_enc_params
Definition: ni_device_api.h:2660
_ni_session_context::last_pkt_pos
uint64_t last_pkt_pos
Definition: ni_device_api.h:1405
_ni_t408_config_t::skipIntraTrans
int32_t skipIntraTrans
Definition: ni_device_api_priv.h:396
_ni_encoder_cfg_params::vbv_max_rate
int vbv_max_rate
Definition: ni_device_api.h:2297
_ni_gop_params::qp_factor
float qp_factor
Definition: ni_device_api.h:1905
_ni_encoder_config_t::ui16aspectRatioHeight
uint16_t ui16aspectRatioHeight
Definition: ni_device_api_priv.h:555
ni_instance_buf_info_rw_type_t
ni_instance_buf_info_rw_type_t
Definition: ni_device_api_priv.h:46
check_err_rc_option_t
check_err_rc_option_t
Definition: ni_device_api_priv.c:92
_ni_xcoder_params::force_frame_type
int force_frame_type
Definition: ni_device_api.h:2637
_ni_gop_rps::ref_pic
int ref_pic
Definition: ni_device_api.h:1897
NI_PIX_FMT_NV12
@ NI_PIX_FMT_NV12
Definition: ni_device_api.h:264
_ni_encoder_config_t::ui8fillerEnable
uint8_t ui8fillerEnable
Definition: ni_device_api_priv.h:561
NI_P2P_RECV
#define NI_P2P_RECV
Definition: ni_device_api_priv.h:786
ni_query_general_status
int ni_query_general_status(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_instance_mgr_general_status_t *p_gen_status)
Query a particular xcoder instance to get GeneralStatus data.
Definition: ni_device_api_priv.c:5763
_ni_nvme_identity_xcoder_hw::hw_codec_type
uint8_t hw_codec_type
Definition: ni_nvme.h:78
_ni_session_stats::ui32LastTransactionCompletionStatus
uint32_t ui32LastTransactionCompletionStatus
Definition: ni_device_api_priv.h:140
NI_H265_USERDATA_FLAG_UNREGISTERED_PRE
@ NI_H265_USERDATA_FLAG_UNREGISTERED_PRE
Definition: ni_device_api.h:353
_ni_frame::data_len
uint32_t data_len[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2560
ni_decoder_session_copy_internal
ni_retcode_t ni_decoder_session_copy_internal(ni_session_context_t *src_p_ctx, ni_session_context_t *dst_p_ctx)
Copy a xcoder decoder worker thread info.
Definition: ni_device_api_priv.c:12742
NI_MAX_SLICE_SIZE
#define NI_MAX_SLICE_SIZE
Definition: ni_device_api.h:219
_ni_session_context::pixel_format_changed
int pixel_format_changed
Definition: ni_device_api.h:1656
_ni_frame::extra_data_len
unsigned int extra_data_len
Definition: ni_device_api.h:2553
DP_IPC_PASSTHRU
#define DP_IPC_PASSTHRU
Definition: ni_device_api_priv.c:169
_ni_encoder_cfg_params::intra_qp
int intra_qp
Definition: ni_device_api.h:2287
_ni_global_session_stats::error_flag
char error_flag[16]
Definition: ni_device_api_priv.h:150
PIC_TYPE_I
@ PIC_TYPE_I
Definition: ni_device_api.h:372
_ni_encoder_cfg_params::enable_rate_control
int enable_rate_control
Definition: ni_device_api.h:2284
_ni_instance_mgr_general_status::fw_video_mem_usage
uint8_t fw_video_mem_usage
Definition: ni_device_api_priv.h:93
ni_p2p_ioctl.h
Definitions related to NETINT P2P kernel driver interface.
_ni_encoder_config_t::ui8multicoreJointMode
uint8_t ui8multicoreJointMode
Definition: ni_device_api_priv.h:567
CONFIG_SESSION_Write_W
#define CONFIG_SESSION_Write_W(sid)
Definition: ni_nvme.h:866
XCODER_MAX_ENC_PIC_WIDTH
#define XCODER_MAX_ENC_PIC_WIDTH
Definition: ni_util.h:88
_ni_encoder_cfg_params::max_consecutive_skip_num
int max_consecutive_skip_num
Definition: ni_device_api.h:2337
NI_PIXEL_PLANAR_MAX
@ NI_PIXEL_PLANAR_MAX
Definition: ni_device_api.h:890
_ni_decoder_input_params_t::semi_planar
int semi_planar[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2408
_ni_decoder_input_params_t::scale_long_short_edge
int scale_long_short_edge[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2436
_ni_encoder_config_t::ui16hdr10_wx
uint16_t ui16hdr10_wx
Definition: ni_device_api_priv.h:591
_ni_nvme_identity::hw1_id
uint8_t hw1_id
Definition: ni_nvme.h:179
_ni_t408_config_t::max_delta_qp
int32_t max_delta_qp
Definition: ni_device_api_priv.h:404
_ni_encoder_config_t::ui16hdr10_dx1
uint16_t ui16hdr10_dx1
Definition: ni_device_api_priv.h:587
_ni_decoder_input_params_t::hwframes
int hwframes
Definition: ni_device_api.h:2402
NI_POOL_TYPE_NORMAL
@ NI_POOL_TYPE_NORMAL
Definition: ni_device_api.h:471
NI_DEVICE_TYPE_AI
@ NI_DEVICE_TYPE_AI
Definition: ni_defs.h:344
ni_hwupload_session_read_hwdesc
int ni_hwupload_session_read_hwdesc(ni_session_context_t *p_ctx, niFrameSurface1_t *hwdesc)
Retrieve a HW descriptor of uploaded frame.
Definition: ni_device_api_priv.c:13126
_ni_ai_config_t
Definition: ni_device_api_priv.h:726
NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
@ NI_RETCODE_PARAM_ERROR_DECODING_REFRESH_TYPE
Definition: ni_defs.h:477
_ni_encoder_config_t::ui8PixelFormat
uint8_t ui8PixelFormat
Definition: ni_device_api_priv.h:558
ni_scaler_session_close
ni_retcode_t ni_scaler_session_close(ni_session_context_t *p_ctx, int eos_received)
close a scaler session
Definition: ni_device_api_priv.c:5125
_ni_uploader_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:649
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_16X16_EN
Definition: ni_defs.h:480
_ni_encoder_cfg_params::multicoreJointMode
int multicoreJointMode
Definition: ni_device_api.h:2217
CHECK_ERR_RC
#define CHECK_ERR_RC(ctx, rc, info, opcode, type, hw_id, inst_id, opt)
Definition: ni_device_api_priv.c:701
ni_timestamp_init
ni_retcode_t ni_timestamp_init(ni_session_context_t *p_ctx, ni_timestamp_table_t **pp_table, const char *name)
Initialize timestamp handling.
Definition: ni_util.c:1290
FP_CORE
@ FP_CORE
Definition: ni_defs.h:365
_ni_hw_capability::max_video_height
uint16_t max_video_height
Definition: ni_device_api.h:1097
_ni_xcoder_params::enable_ai_enhance
int enable_ai_enhance
Definition: ni_device_api.h:2676
NETINT_PCI_VENDOR_ID
#define NETINT_PCI_VENDOR_ID
Definition: ni_defs.h:121
ni_calculate_sha256
void ni_calculate_sha256(const uint8_t aui8Data[], size_t ui32DataLength, uint8_t aui8Hash[])
Definition: ni_util.c:3532
NI_RETCODE_ERROR_NVME_CMD_FAILED
@ NI_RETCODE_ERROR_NVME_CMD_FAILED
Definition: ni_defs.h:425
_ni_t408_config_t::cu16MergeDeltaRate
int32_t cu16MergeDeltaRate
Definition: ni_device_api_priv.h:472
_ni_thread_arg_struct_t::device_type
uint32_t device_type
Definition: ni_device_api.h:1214
NI_HDR10P_SEI_BYTE3
#define NI_HDR10P_SEI_BYTE3
Definition: ni_device_api.h:442
_ni_decoder_config_t::ui8MCMode
uint8_t ui8MCMode
Definition: ni_device_api_priv.h:712
_ni_decoder_input_params_t
Definition: ni_device_api.h:2352
_ni_t408_config_t::nrCbEnable
uint32_t nrCbEnable
Definition: ni_device_api_priv.h:420
_ni_encoder_config_t::ui32vbvMinRate
uint32_t ui32vbvMinRate
Definition: ni_device_api_priv.h:634
_ni_encoder_config_t::i32frameRateInfo
int32_t i32frameRateInfo
Definition: ni_device_api_priv.h:510
_ni_t408_config_t::bgThrDiff
uint32_t bgThrDiff
Definition: ni_device_api_priv.h:444
GOP_PRESET_IDX_CUSTOM
@ GOP_PRESET_IDX_CUSTOM
Definition: ni_device_api_priv.h:316
_ni_encoder_config_t::ui8AiEnhanceMode
uint8_t ui8AiEnhanceMode
Definition: ni_device_api_priv.h:607
QUERY_INSTANCE_EOS_R
#define QUERY_INSTANCE_EOS_R(sid, instance)
Definition: ni_nvme.h:720
_ni_t408_config_t::dependSliceMode
int32_t dependSliceMode
Definition: ni_device_api_priv.h:382
_ni_encoder_cfg_params::HDR10dy0
int HDR10dy0
Definition: ni_device_api.h:2203
_ni_load_query::fw_video_mem_usage
uint32_t fw_video_mem_usage
Definition: ni_device_api.h:1157
ni_pthread_cond_timedwait
int ni_pthread_cond_timedwait(ni_pthread_cond_t *cond, ni_pthread_mutex_t *mutex, const struct timespec *abstime)
wait on a condition
Definition: ni_util.c:4257
_ni_encoder_cfg_params::enable_dynamic_8x8_merge
int enable_dynamic_8x8_merge
Definition: ni_device_api.h:2269
_ni_t408_config_t::profile
int32_t profile
Definition: ni_device_api_priv.h:366
_ni_scaler_params_t::nb_inputs
int nb_inputs
Definition: ni_device_api.h:2468
_ni_encoder_cfg_params::ctbRcMode
int ctbRcMode
Definition: ni_device_api.h:2250
ni_encoder_frame_params_t
struct _ni_encoder_frame_params ni_encoder_frame_params_t
_ni_t408_config_t::entropy_coding_mode
int32_t entropy_coding_mode
Definition: ni_device_api_priv.h:494
_ni_thread_arg_struct_t::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1219
NI_MEM_PAGE_ALIGNMENT
#define NI_MEM_PAGE_ALIGNMENT
Definition: ni_defs.h:246
NI_LOG_TRACE
@ NI_LOG_TRACE
Definition: ni_log.h:63
_ni_encoder_cfg_params::long_term_ref_interval
int long_term_ref_interval
Definition: ni_device_api.h:2219
ni_decoder_session_open
ni_retcode_t ni_decoder_session_open(ni_session_context_t *p_ctx)
Open a xcoder decoder instance.
Definition: ni_device_api_priv.c:1100
_ni_frame::end_of_stream
uint32_t end_of_stream
Definition: ni_device_api.h:2504
_ni_session_context::network_data
ni_network_data_t * network_data
Definition: ni_device_api.h:1629
NI_T35_SEI_HDR10_PLUS
@ NI_T35_SEI_HDR10_PLUS
Definition: ni_device_api_priv.c:89
_ni_frame_config::orientation
uint8_t orientation
Definition: ni_device_api.h:2716
NI_HWDESC_UNIFIED_MEMBIN_SIZE
#define NI_HWDESC_UNIFIED_MEMBIN_SIZE
Definition: ni_device_api_priv.h:779
CONFIG_SESSION_SWVersion_W
#define CONFIG_SESSION_SWVersion_W(sid)
Definition: ni_nvme.h:879
NI_DEC_FRAME_BUF_POOL_SIZE_INIT
#define NI_DEC_FRAME_BUF_POOL_SIZE_INIT
Definition: ni_util.h:91
_ni_hw_capability::video_profile
uint8_t video_profile
Definition: ni_device_api.h:1100
NI_CODEC_HW_DOWNLOAD
@ NI_CODEC_HW_DOWNLOAD
Definition: ni_device_api.h:908
_ni_encoder_config_t::ui8colorSpace
uint8_t ui8colorSpace
Definition: ni_device_api_priv.h:553
_ni_metadata_enc_bstream::frame_size
uint16_t frame_size
Definition: ni_device_api_priv.h:291
_ni_encoder_frame_params::data_format
uint16_t data_format
Definition: ni_device_api_priv.h:199
ni_pthread_mutex_lock
int ni_pthread_mutex_lock(ni_pthread_mutex_t *mutex)
thread mutex lock
Definition: ni_util.c:4043
_niFrameSurface1::encoding_type
int8_t encoding_type
Definition: ni_device_api.h:2696
ni_hwdownload_session_read
int ni_hwdownload_session_read(ni_session_context_t *p_ctx, ni_frame_t *p_frame, niFrameSurface1_t *hwdesc)
Retrieve a YUV p_frame from decoder.
Definition: ni_device_api_priv.c:14102
_ni_encoder_config_t::i32hwframes
int32_t i32hwframes
Definition: ni_device_api_priv.h:534
_ni_frameclone_desc::ui16SrcIdx
uint16_t ui16SrcIdx
Definition: ni_device_api.h:1328
_ni_xcoder_params::hwframes
int hwframes
Definition: ni_device_api.h:2669
_ni_session_context::prev_size
int prev_size
Definition: ni_device_api.h:1458
_ni_encoder_cfg_params::newRcEnable
int newRcEnable
Definition: ni_device_api.h:2310
_ni_session_context::device_handle
ni_device_handle_t device_handle
Definition: ni_device_api.h:1413
_ni_session_context::p_session_config
void * p_session_config
Definition: ni_device_api.h:1424
NI_CODEC_FORMAT_AV1
@ NI_CODEC_FORMAT_AV1
Definition: ni_device_api.h:882
NI_MAX_TX_RETRIES
#define NI_MAX_TX_RETRIES
Definition: ni_device_api.h:64
_ni_encoder_cfg_params::skip_frame_enable
int skip_frame_enable
Definition: ni_device_api.h:2336
ni_rsrc_free_device_context
void ni_rsrc_free_device_context(ni_device_context_t *p_device_context)
Free previously allocated device context.
Definition: ni_rsrc_api.cpp:1194
_ni_frame::sei_hdr_plus_len
unsigned int sei_hdr_plus_len
Definition: ni_device_api.h:2537
_ni_xcoder_params::preset
int preset
Definition: ni_device_api.h:2611
NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_rc
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_rc
Definition: ni_device_api_priv.h:800
_ni_metadata_dec_frame::sei_header
uint32_t sei_header
Definition: ni_device_api_priv.h:230
_ni_encoder_frame_params::timestamp
uint32_t timestamp
Definition: ni_device_api_priv.h:203
NI_SCALER_OPCODE_SCALE
@ NI_SCALER_OPCODE_SCALE
Definition: ni_defs.h:565
_ni_session_context::dec_fme_buf_pool
ni_buf_pool_t * dec_fme_buf_pool
Definition: ni_device_api.h:1510
NI_NOPTS_VALUE
#define NI_NOPTS_VALUE
Definition: ni_device_api.h:52
_ni_metadata_enc_bstream_rev61::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api_priv.h:268
_ni_custom_sei_set
Definition: ni_device_api.h:1080
_ni_encoder_cfg_params::entropy_coding_mode
int entropy_coding_mode
Definition: ni_device_api.h:2235
_ni_metadata_common::frame_offset
uint64_t frame_offset
Definition: ni_device_api_priv.h:215
_ni_packet::p_data
void * p_data
Definition: ni_device_api.h:2731
INST_BUF_INFO_RW_WRITE_BY_EP
@ INST_BUF_INFO_RW_WRITE_BY_EP
Definition: ni_device_api_priv.h:54
_ni_hw_capability::min_video_width
uint16_t min_video_width
Definition: ni_device_api.h:1098
_ni_session_data_io
Definition: ni_device_api.h:2750
_ni_t408_config_t::conf_win_left
int32_t conf_win_left
Definition: ni_device_api_priv.h:378
ni_validate_custom_dec_template
ni_retcode_t ni_validate_custom_dec_template(ni_xcoder_params_t *p_src, ni_session_context_t *p_ctx, ni_decoder_config_t *p_cfg, char *p_param_err, uint32_t max_err_len)
Perform validation on custom dec parameters (Rev. B)
Definition: ni_device_api_priv.c:9656
_ni_encoder_config_t::ui8temporalLayersEnable
uint8_t ui8temporalLayersEnable
Definition: ni_device_api_priv.h:606
ni_uploader_session_open
ni_retcode_t ni_uploader_session_open(ni_session_context_t *p_ctx)
Open a xcoder upload instance.
Definition: ni_device_api_priv.c:12541
_ni_metadata_enc_bstream::reconLumaSize
uint32_t reconLumaSize
Definition: ni_device_api_priv.h:297
NI_RETCODE_NVME_SC_VPU_RECOVERY
@ NI_RETCODE_NVME_SC_VPU_RECOVERY
Definition: ni_defs.h:539
QUERY_INSTANCE_RBUFF_SIZE_BUSY_R
#define QUERY_INSTANCE_RBUFF_SIZE_BUSY_R(sid, instance)
Definition: ni_nvme.h:728
_ni_encoder_cfg_params::ltrFirstGap
int ltrFirstGap
Definition: ni_device_api.h:2215
TP_CORE
@ TP_CORE
Definition: ni_defs.h:364
_ni_encoder_frame_params::force_picture_type
uint16_t force_picture_type
Definition: ni_device_api_priv.h:198
_ni_content_light_level_info_bytes::max_pic_average_light_level
uint16_t max_pic_average_light_level
Definition: ni_device_api.h:1044
_ni_encoder_cfg_params::ltrRefQpOffset
int ltrRefQpOffset
Definition: ni_device_api.h:2214
_ni_t408_config_t::chromaCrQpOffset
int32_t chromaCrQpOffset
Definition: ni_device_api_priv.h:415
_ni_network_buffer::ui8MultiIn
uint8_t ui8MultiIn
Definition: ni_device_api_priv.h:739
NI_PIX_FMT_YUV420P10LE
@ NI_PIX_FMT_YUV420P10LE
Definition: ni_device_api.h:263
_ni_frame_config::rgba_color
uint32_t rgba_color
Definition: ni_device_api.h:2712
ni_log
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition: ni_log.c:183
ni_ai_session_close
ni_retcode_t ni_ai_session_close(ni_session_context_t *p_ctx, int eos_recieved)
Definition: ni_device_api_priv.c:15969
_ni_metadata_common::pkt_delay_cnt
uint8_t pkt_delay_cnt
Definition: ni_device_api_priv.h:222
NI_XCODER_REVISION_API_MAJOR_VER_IDX
#define NI_XCODER_REVISION_API_MAJOR_VER_IDX
Definition: ni_defs.h:96
_ni_t408_config_t::slice_arg
int32_t slice_arg
Definition: ni_device_api_priv.h:490
_ni_encoder_cfg_params::enable_transform_8x8
int enable_transform_8x8
Definition: ni_device_api.h:2275
_ni_session_context::sender_handle
ni_device_handle_t sender_handle
Definition: ni_device_api.h:1419
ni_hwdownload_by_frame_idx
int ni_hwdownload_by_frame_idx(niFrameSurface1_t *hwdesc, ni_frame_t *p_frame, int is_auto_dl)
Retrieve a YUV p_frame by frame index.
Definition: ni_device_api_priv.c:14294
_ni_scaler_config::filterblit
uint8_t filterblit
Definition: ni_device_api_priv.h:745
CONFIG_INSTANCE_SetP2P_W
#define CONFIG_INSTANCE_SetP2P_W(sid, instance)
Definition: ni_nvme.h:855
_ni_session_statistic_t::ui32FramesOutput
uint32_t ui32FramesOutput
Definition: ni_defs.h:377
_ni_dec_mastering_display_colour_volume_bytes::min_display_mastering_luminance
uint32_t min_display_mastering_luminance
Definition: ni_device_api.h:1034
NI_MAX_GOP_NUM
#define NI_MAX_GOP_NUM
Definition: ni_device_api.h:57
_ni_t408_config_t::monochromeEnable
uint32_t monochromeEnable
Definition: ni_device_api_priv.h:439
ni_query_instance_buf_info
ni_retcode_t ni_query_instance_buf_info(ni_session_context_t *p_ctx, ni_instance_buf_info_rw_type_t rw_type, ni_device_type_t device_type, ni_instance_buf_info_t *p_inst_buf_info)
Query a particular xcoder instance to get buffer/data Info data.
Definition: ni_device_api_priv.c:6443
SwapSW32
void SwapSW32(uint32_t *buf, uint32_t bytes)
Definition: ni_device_api_priv.c:200
_ni_encoder_config_t::i8crfDecimal
int8_t i8crfDecimal
Definition: ni_device_api_priv.h:630
ni_create_frame
int ni_create_frame(ni_frame_t *p_frame, uint32_t read_length, uint64_t *p_frame_offset, bool is_hw_frame)
Get info from received p_frame.
Definition: ni_device_api_priv.c:7169
_ni_session_context::keep_alive_timeout
uint32_t keep_alive_timeout
Definition: ni_device_api.h:1472
_ni_encoder_config_t::ui8bitstreamFormat
uint8_t ui8bitstreamFormat
Definition: ni_device_api_priv.h:505
_ni_session_statistic_t::ui8AdditionalFramesDelay
uint8_t ui8AdditionalFramesDelay
Definition: ni_defs.h:391
NI_HDR10P_SEI_BYTE2
#define NI_HDR10P_SEI_BYTE2
Definition: ni_device_api.h:441
NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
@ NI_RETCODE_PARAM_ERROR_CUSIZE_MODE_8X8_EN
Definition: ni_defs.h:479
ni_ai_session_read
ni_retcode_t ni_ai_session_read(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:15402
NI_SCALER_OPCODE_STACK
@ NI_SCALER_OPCODE_STACK
Definition: ni_defs.h:570
_ni_encoder_cfg_params
Definition: ni_device_api.h:2002
GC620_BGRX8888
#define GC620_BGRX8888
Definition: ni_device_api.h:201
NI_RETCODE_PARAM_WARN
@ NI_RETCODE_PARAM_WARN
Definition: ni_defs.h:519
_ni_encoder_config_t::ui8repeatHeaders
uint8_t ui8repeatHeaders
Definition: ni_device_api_priv.h:543
_ni_encoder_cfg_params::EnableAUD
int EnableAUD
Definition: ni_device_api.h:2229
_ni_nvme_identity::hw1_codec_type
uint8_t hw1_codec_type
Definition: ni_nvme.h:183
_ni_session_context::pkt_offsets_index
uint64_t pkt_offsets_index[NI_FIFO_SZ]
Definition: ni_device_api.h:1402
ni_fmt_fw_api_ver_str
void ni_fmt_fw_api_ver_str(const char ver_str[], char fmt_str[])
Get formatted FW API version string from unformatted FW API version string.
Definition: ni_util.c:3626
FRAME_CHUNK_INDEX_SIZE
#define FRAME_CHUNK_INDEX_SIZE
Definition: ni_device_api_priv.h:774
ni_usleep
void ni_usleep(int64_t usec)
Definition: ni_util.c:340
_ni_encoder_config_t::ui8useLowDelayPocType
uint8_t ui8useLowDelayPocType
Definition: ni_device_api_priv.h:546
ni_ai_multi_config_frame
ni_retcode_t ni_ai_multi_config_frame(ni_session_context_t *p_ctx, ni_frame_config_t p_cfg_in[], int numInCfgs, ni_frame_config_t *p_cfg_out)
Definition: ni_device_api_priv.c:16074
ni_network_buffer_t
struct _ni_network_buffer ni_network_buffer_t
CONFIG_SESSION_KeepAlive_W
#define CONFIG_SESSION_KeepAlive_W(sid)
Definition: ni_nvme.h:862
_ni_session_context::device_type
uint32_t device_type
Definition: ni_device_api.h:1435
_ni_decoder_config_t::ui32MaxPktSize
uint32_t ui32MaxPktSize
Definition: ni_device_api_priv.h:716
niFrameSurface1_t
struct _niFrameSurface1 niFrameSurface1_t
NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
@ NI_RETCODE_PARAM_ERROR_MX_DELTA_QP
Definition: ni_defs.h:456
_ni_t408_config_t::intra_period
int32_t intra_period
Definition: ni_device_api_priv.h:375
ni_set_default_template
void ni_set_default_template(ni_session_context_t *p_ctx, ni_encoder_config_t *p_config)
Setup and initialize all xcoder configuration to default (Rev. B)
Definition: ni_device_api_priv.c:9288
NI_ENC_MIN_RESOLUTION_WIDTH
#define NI_ENC_MIN_RESOLUTION_WIDTH
Definition: ni_device_api.h:88
QUERY_INSTANCE_CUR_STATUS_INFO_R
#define QUERY_INSTANCE_CUR_STATUS_INFO_R(sid, instance)
Definition: ni_nvme.h:711
NI_AI_HW_ALIGN_SIZE
#define NI_AI_HW_ALIGN_SIZE
Definition: ni_device_api_priv.h:1178
ni_decoder_output_config_t::ui8SemiPlanarEnabled
uint8_t ui8SemiPlanarEnabled
Definition: ni_device_api_priv.h:693
_ni_t408_config_t::minQpB
int32_t minQpB
Definition: ni_device_api_priv.h:481
_ni_metadata_common::frame_width
uint16_t frame_width
Definition: ni_device_api_priv.h:218
QUERY_INSTANCE_NL_SIZE_R
#define QUERY_INSTANCE_NL_SIZE_R(sid, instance)
Definition: ni_nvme.h:752
GC620_RGBA8888
#define GC620_RGBA8888
Definition: ni_device_api.h:199
_ni_frame::force_key_frame
int force_key_frame
Definition: ni_device_api.h:2520
NI_GET_MAX_HWDESC_FRAME_INDEX
#define NI_GET_MAX_HWDESC_FRAME_INDEX(x)
Definition: ni_defs.h:273
_ni_global_session_stats::check_flag
uint8_t check_flag[4]
Definition: ni_device_api_priv.h:151
LRETURN
#define LRETURN
Definition: ni_defs.h:318
NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
@ NI_PIXEL_PLANAR_FORMAT_SEMIPLANAR
Definition: ni_device_api.h:887
calculate_psnr
void calculate_psnr(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:450
_ni_t408_config_t::numUnitsInTick
uint32_t numUnitsInTick
Definition: ni_device_api_priv.h:410
ni_instance_mgr_allocation_info_t
struct _ni_instance_mgr_allocation_info ni_instance_mgr_allocation_info_t
_ni_instance_mgr_general_status::fw_model_load_overall
uint32_t fw_model_load_overall
Definition: ni_device_api_priv.h:104
_ni_nvme_identity::hw2_video_profile
uint8_t hw2_video_profile
Definition: ni_nvme.h:201
_ni_encoder_cfg_params::HDR10CLLEnable
int HDR10CLLEnable
Definition: ni_device_api.h:2200
_ni_metadata_enc_frame::force_headers
uint16_t force_headers
Definition: ni_device_api_priv.h:252
_ni_encoder_config_t::ui8EnableRdoQuant
uint8_t ui8EnableRdoQuant
Definition: ni_device_api_priv.h:542
_ni_t408_config_t::pu08IntraPlanarDeltaRate
int32_t pu08IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:458
_ni_frameclone_desc::ui32Offset
uint32_t ui32Offset
Definition: ni_device_api.h:1330
_ni_encoder_config_t::ui16hdr10_dy1
uint16_t ui16hdr10_dy1
Definition: ni_device_api_priv.h:588
NI_HDR10P_SEI_BYTE0
#define NI_HDR10P_SEI_BYTE0
Definition: ni_device_api.h:439
_ni_content_light_level_info_bytes::max_content_light_level
uint16_t max_content_light_level
Definition: ni_device_api.h:1043
_ni_packet::video_height
uint32_t video_height
Definition: ni_device_api.h:2728
ni_fix_VUI
void ni_fix_VUI(uint8_t *vui, int pos, int value)
insert the 32 bits of integer value at bit position pos
Definition: ni_device_api_priv.c:7942
_ni_session_context::buffer_pool
ni_queue_buffer_pool_t * buffer_pool
Definition: ni_device_api.h:1509
_ni_xcoder_params::chroma_linesize
int chroma_linesize
Definition: ni_device_api.h:2680
NI_MINIMUM_CROPPED_LENGTH
#define NI_MINIMUM_CROPPED_LENGTH
Definition: ni_device_api_priv.h:669
_ni_xcoder_params::minFramesDelay
int minFramesDelay
Definition: ni_device_api.h:2683
_ni_xcoder_params::sar_num
int sar_num
Definition: ni_device_api.h:2654
_ni_metadata_enc_frame::metadata_common
ni_metadata_common_t metadata_common
Definition: ni_device_api_priv.h:241
_ni_t408_config_t::pu16IntraAngleDeltaRate
int32_t pu16IntraAngleDeltaRate
Definition: ni_device_api_priv.h:463
_ni_t408_config_t::constIntraPredFlag
int32_t constIntraPredFlag
Definition: ni_device_api_priv.h:371
_ni_metadata_enc_frame::frame_roi_map_size
uint32_t frame_roi_map_size
Definition: ni_device_api_priv.h:243
NI_RETCODE_PARAM_ERROR_HVS_QP_EN
@ NI_RETCODE_PARAM_ERROR_HVS_QP_EN
Definition: ni_defs.h:450
ni_config_instance_eos
ni_retcode_t ni_config_instance_eos(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Send a p_config command for End Of Stream.
Definition: ni_device_api_priv.c:6721
_ni_packet
Definition: ni_device_api.h:2719
NI_DATA_BUFFER_LEN
#define NI_DATA_BUFFER_LEN
Definition: ni_nvme.h:623
_ni_session_statistic_t::ui32RdBufAvailSize
uint32_t ui32RdBufAvailSize
Definition: ni_defs.h:372
_ni_session_config_rw::ui8HWAccess
uint8_t ui8HWAccess
Definition: ni_device_api_priv.h:71
_ni_nvme_identity::ai8Sn
uint8_t ai8Sn[20]
Definition: ni_nvme.h:95
NI_MAX_P2P_SGL_ENTRIES
#define NI_MAX_P2P_SGL_ENTRIES
Definition: ni_device_api_priv.h:788
_ni_session_context::frame_num
uint64_t frame_num
Definition: ni_device_api.h:1482
_ni_frame::sei_cc_len
unsigned int sei_cc_len
Definition: ni_device_api.h:2529
ni_device_session_context_clear
void ni_device_session_context_clear(ni_session_context_t *p_ctx)
Clear already allocated session context.
Definition: ni_device_api.c:248
_ni_encoder_cfg_params::HDR10Enable
int HDR10Enable
Definition: ni_device_api.h:2201
CONFIG_INSTANCE_SetEncFramePara_W
#define CONFIG_INSTANCE_SetEncFramePara_W(sid, instance)
Definition: ni_nvme.h:838
_ni_queue_t::count
uint32_t count
Definition: ni_device_api.h:1267
_ni_encoder_cfg_params::intra_qp_delta
int intra_qp_delta
Definition: ni_device_api.h:2288
_ni_session_context::ori_luma_linesize
int ori_luma_linesize
Definition: ni_device_api.h:1631
_ni_xcoder_params::ai_enhance_level
int ai_enhance_level
Definition: ni_device_api.h:2681
_ni_network_layer_params_t::sizes
uint32_t sizes[6]
Definition: ni_device_api.h:1280
_ni_t408_config_t::enable_mb_level_rc
int32_t enable_mb_level_rc
Definition: ni_device_api_priv.h:493
ni_decoder_session_send_eos
ni_retcode_t ni_decoder_session_send_eos(ni_session_context_t *p_ctx)
Send end of stream signal to the decoder.
Definition: ni_device_api_priv.c:1443
_ni_network_layer_offset::offset
int32_t offset
Definition: ni_device_api.h:1311
_ni_t408_config_t::cu16IntraDeltaRate
int32_t cu16IntraDeltaRate
Definition: ni_device_api_priv.h:470
_ni_instance_mgr_stream_info::is_flushed
uint16_t is_flushed
Definition: ni_device_api_priv.h:129
_ni_encoder_cfg_params::colorDescPresent
int colorDescPresent
Definition: ni_device_api.h:2239
_ni_t408_config_t::wppEnable
int32_t wppEnable
Definition: ni_device_api_priv.h:390
ni_ai_config_t
struct _ni_ai_config_t ni_ai_config_t
CONFIG_INSTANCE_SetEncPara_W
#define CONFIG_INSTANCE_SetEncPara_W(sid, instance)
Definition: ni_nvme.h:834
_ni_overall_load_query::overall_fw_model_load
uint32_t overall_fw_model_load
Definition: ni_device_api.h:1179
_ni_device_context
Definition: ni_rsrc_api.h:145
_ni_decoder_config_t::fps_number
uint32_t fps_number
Definition: ni_device_api_priv.h:710
_ni_session_context::last_gop_size
int last_gop_size
Definition: ni_device_api.h:1642
nvme_admin_cmd_xcoder_config
@ nvme_admin_cmd_xcoder_config
Definition: ni_nvme.h:324
_ni_frame::vui_offset
unsigned int vui_offset
Definition: ni_device_api.h:2545
_ni_encoder_cfg_params::tolCtbRcInter
float tolCtbRcInter
Definition: ni_device_api.h:2254
ni_recv_from_target
ni_retcode_t ni_recv_from_target(ni_session_context_t *p_ctx, const ni_p2p_sgl_t *dmaAddrs, ni_frame_t *pDstFrame)
Definition: ni_device_api_priv.c:17212
_ni_frame::p_data
uint8_t * p_data[NI_MAX_NUM_DATA_POINTERS]
Definition: ni_device_api.h:2559
_ni_encoder_cfg_params::custom_gop_params
ni_custom_gop_params_t custom_gop_params
Definition: ni_device_api.h:2183
_ni_encoder_cfg_params::HDR10dx0
int HDR10dx0
Definition: ni_device_api.h:2202
NVME_LOG_OFFSET_IN_4K
#define NVME_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:660
_ni_xcoder_params::zerocopy_mode
int zerocopy_mode
Definition: ni_device_api.h:2678
_ni_xcoder_params::sar_denom
int sar_denom
Definition: ni_device_api.h:2655
_ni_t408_config_t::cu32IntraDeltaRate
int32_t cu32IntraDeltaRate
Definition: ni_device_api_priv.h:473
ni_decoder_output_config_t::sCroppingRectable
ni_decode_cropping_rectangle sCroppingRectable
Definition: ni_device_api_priv.h:701
_ni_metadata_common::crop_bottom
uint16_t crop_bottom
Definition: ni_device_api_priv.h:212
_ni_decoder_input_params_t::max_extra_hwframe_cnt
int max_extra_hwframe_cnt
Definition: ni_device_api.h:2430
NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
@ NI_RETCODE_PARAM_ERROR_MAXNUMMERGE
Definition: ni_defs.h:470
_ni_device_capability::fw_build_time
uint8_t fw_build_time[26]
Definition: ni_device_api.h:1123
_ni_encoder_config_t::ui32sourceEndian
uint32_t ui32sourceEndian
Definition: ni_device_api_priv.h:528
_ni_decoder_config_t::asOutputConfig
ni_decoder_output_config_t asOutputConfig[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api_priv.h:717
ni_check_ratecontrol_params
ni_retcode_t ni_check_ratecontrol_params(ni_encoder_config_t *p_cfg, char *p_param_err, uint32_t max_err_len)
Definition: ni_device_api_priv.c:12150
_ni_t408_config_t::nrIntraWeightCr
uint32_t nrIntraWeightCr
Definition: ni_device_api_priv.h:426
_ni_session_context::pkt_index
int pkt_index
Definition: ni_device_api.h:1401
_ni_encoder_cfg_params::disableBframeRdoq
int disableBframeRdoq
Definition: ni_device_api.h:2345
NI_CODEC_FORMAT_VP9
@ NI_CODEC_FORMAT_VP9
Definition: ni_device_api.h:880
GOP_PRESET_IDX_HIERARCHICAL_IPPPP
@ GOP_PRESET_IDX_HIERARCHICAL_IPPPP
Definition: ni_device_api_priv.h:327
_ni_t408_config_t::nrCrEnable
uint32_t nrCrEnable
Definition: ni_device_api_priv.h:421
ni_core_type_t
ni_core_type_t
Definition: ni_defs.h:358
NI_DEVICE_TYPE_SCALER
@ NI_DEVICE_TYPE_SCALER
Definition: ni_defs.h:343
_ni_nvme_identity_xcoder_hw::hw_video_profile
uint8_t hw_video_profile
Definition: ni_nvme.h:83
_ni_instance_mgr_stream_info::frame_rate
uint16_t frame_rate
Definition: ni_device_api_priv.h:128
_ni_encoder_cfg_params::tolCtbRcIntra
float tolCtbRcIntra
Definition: ni_device_api.h:2255
_ni_t408_config_t::cu08MergeDeltaRate
int32_t cu08MergeDeltaRate
Definition: ni_device_api_priv.h:469
_ni_session_statistic_t::ui32LastErrorTransactionId
uint32_t ui32LastErrorTransactionId
Definition: ni_defs.h:385
_ni_instance_mgr_stream_info::picture_height
uint16_t picture_height
Definition: ni_device_api_priv.h:125
XCODER_MAX_ENC_PIC_HEIGHT
#define XCODER_MAX_ENC_PIC_HEIGHT
Definition: ni_util.h:89
NI_DDR_PRIORITY_RESET
@ NI_DDR_PRIORITY_RESET
Definition: ni_device_api.h:1767
_ni_frame::roi_len
unsigned int roi_len
Definition: ni_device_api.h:2549
_ni_encoder_config_t::ui8gopSize
uint8_t ui8gopSize
Definition: ni_device_api_priv.h:545
ni_dec_fme_buffer_pool_initialize
int32_t ni_dec_fme_buffer_pool_initialize(ni_session_context_t *p_ctx, int32_t number_of_buffers, int width, int height, int height_align, int factor)
Definition: ni_util.c:608
_ni_session_context::last_pts_interval
int64_t last_pts_interval
Definition: ni_device_api.h:1591
ni_encoder_session_close
ni_retcode_t ni_encoder_session_close(ni_session_context_t *p_ctx, int eos_recieved)
Close a xcoder encoder instance.
Definition: ni_device_api_priv.c:3680
NI_RETCODE_PARAM_ERROR_CONF_WIN_R
@ NI_RETCODE_PARAM_ERROR_CONF_WIN_R
Definition: ni_defs.h:464
_ni_metadata_common::crop_top
uint16_t crop_top
Definition: ni_device_api_priv.h:211
NI_ERRNO
#define NI_ERRNO
Definition: ni_defs.h:212
_ni_frame_config::picture_height
uint16_t picture_height
Definition: ni_device_api.h:2705
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_BIG
Definition: ni_defs.h:487
_ni_encoder_config_t::ui8LowDelay
uint8_t ui8LowDelay
Definition: ni_device_api_priv.h:580
_ni_frame::sei_hdr_plus_offset
unsigned int sei_hdr_plus_offset
Definition: ni_device_api.h:2536
_ni_encoder_cfg_params::enable_dynamic_16x16_merge
int enable_dynamic_16x16_merge
Definition: ni_device_api.h:2270
ni_xcoder_resource_recovery
@ ni_xcoder_resource_recovery
Definition: ni_device_api_priv.h:63
_ni_nvme_identity::xcoder_devices
ni_nvme_identity_xcoder_hw_t xcoder_devices[NI_MAX_DEVICES_PER_HW_INSTANCE]
Definition: ni_nvme.h:232
NI_RETCODE_PARAM_ERROR_LOOK_AHEAD_DEPTH
@ NI_RETCODE_PARAM_ERROR_LOOK_AHEAD_DEPTH
Definition: ni_defs.h:510
_ni_encoder_config_t::ui32cropHeight
uint32_t ui32cropHeight
Definition: ni_device_api_priv.h:612
ni_t35_sei_mesg_type_t
enum _ni_t35_sei_mesg_type ni_t35_sei_mesg_type_t
_ni_t408_config_t::pu16IntraPlanarDeltaRate
int32_t pu16IntraPlanarDeltaRate
Definition: ni_device_api_priv.h:461
_ni_t408_config_t::enable_transform_8x8
int32_t enable_transform_8x8
Definition: ni_device_api_priv.h:488
_ni_session_context::hw_action
int hw_action
Definition: ni_device_api.h:1560
_ni_input_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:1352
_ni_encoder_config_t::niParamT408
ni_t408_config_t niParamT408
Definition: ni_device_api_priv.h:525
nvme_admin_cmd_xcoder_query
@ nvme_admin_cmd_xcoder_query
Definition: ni_nvme.h:320
_ni_session_context
Definition: ni_device_api.h:1359
_ni_session_context::itu_t_t35_cc_sei_hdr_hevc
uint8_t itu_t_t35_cc_sei_hdr_hevc[NI_CC_SEI_HDR_HEVC_LEN]
Definition: ni_device_api.h:1368
QUERY_DETAIL_GET_STATUS_V1_R
#define QUERY_DETAIL_GET_STATUS_V1_R(instance)
Definition: ni_nvme.h:786
_ni_encoder_cfg_params::iframe_size_ratio
int iframe_size_ratio
Definition: ni_device_api.h:2341
ni_get_planar_from_pixfmt
int ni_get_planar_from_pixfmt(int pix_fmt)
Grab planar info from NI_PIX_FMT.
Definition: ni_device_api_priv.c:14865
current_time
struct timeval start_time previous_time current_time
Definition: ni_device_test.c:236
NI_PIX_FMT_P010LE
@ NI_PIX_FMT_P010LE
Definition: ni_device_api.h:265
ni_memfree
#define ni_memfree(p_memptr)
Definition: ni_util.h:365
_ni_session_context::bus
unsigned short bus
Definition: ni_device_api.h:1571
_ni_thread_arg_struct_t::hw_id
int hw_id
Definition: ni_device_api.h:1210
ni_validate_custom_template
ni_retcode_t ni_validate_custom_template(ni_session_context_t *p_ctx, ni_encoder_config_t *p_cfg, ni_xcoder_params_t *p_src, char *p_param_err, uint32_t max_err_len)
Perform validation on custom parameters (Rev. B)
Definition: ni_device_api_priv.c:9946
NI_CODEC_HW_UPLOAD
@ NI_CODEC_HW_UPLOAD
Definition: ni_device_api.h:909
_niFrameSurface1
Definition: ni_device_api.h:2687
NI_CODEC_HW_ENABLE
@ NI_CODEC_HW_ENABLE
Definition: ni_device_api.h:907
ni_sei_header_t
struct _ni_sei_header ni_sei_header_t
_ni_t408_config_t::coefClearDisable
int32_t coefClearDisable
Definition: ni_device_api_priv.h:476
_ni_metadata_enc_bstream_rev61::frame_type
uint32_t frame_type
Definition: ni_device_api_priv.h:265
_ni_t408_config_t::cu08InterDeltaRate
int32_t cu08InterDeltaRate
Definition: ni_device_api_priv.h:468
NI_PARAM_AV1_MAX_HEIGHT
#define NI_PARAM_AV1_MAX_HEIGHT
Definition: ni_device_api.h:146
ni_decode_cropping_rectangle::ui16H
uint16_t ui16H
Definition: ni_device_api_priv.h:680
_ni_session_context::last_dts_interval
int64_t last_dts_interval
Definition: ni_device_api.h:1590
ni_nvme_send_read_cmd
int32_t ni_nvme_send_read_cmd(ni_device_handle_t handle, ni_event_handle_t event_handle, void *p_data, uint32_t data_len, uint32_t lba)
Compose an io read command.
Definition: ni_nvme.c:523
_ni_metadata_dec_frame::metadata_common
ni_metadata_common_t metadata_common
Definition: ni_device_api_priv.h:228
_ni_ddr_priority_config
Definition: ni_device_api_priv.h:751
_ni_encoder_config_t::ui32QLevel
uint32_t ui32QLevel
Definition: ni_device_api_priv.h:570
_ni_t408_config_t::pu16DeltaRate
int32_t pu16DeltaRate
Definition: ni_device_api_priv.h:453
_ni_session_context::blk_xcoder_name
char blk_xcoder_name[MAX_CHAR_IN_DEVICE_NAME]
Definition: ni_device_api.h:1441
_ni_frame
Definition: ni_device_api.h:2497
_ni_decoder_config_t::ui8DisablePictureReordering
uint8_t ui8DisablePictureReordering
Definition: ni_device_api_priv.h:713
_ni_metadata_dec_frame::sei_number
uint16_t sei_number
Definition: ni_device_api_priv.h:233
ni_pthread_sigmask
int ni_pthread_sigmask(int how, const ni_sigset_t *set, ni_sigset_t *oldset)
examine and change mask of blocked signals
Definition: ni_util.c:4291
_ni_t408_config_t::intraNxNEnable
int32_t intraNxNEnable
Definition: ni_device_api_priv.h:398
_ni_network_data::input_num
uint32_t input_num
Definition: ni_device_api.h:1316
_ni_packet::sent_size
int sent_size
Definition: ni_device_api.h:2733
_ni_encoder_cfg_params::HDR10dy1
int HDR10dy1
Definition: ni_device_api.h:2205
ni_instance_mgr_stream_info_t
struct _ni_instance_mgr_stream_info ni_instance_mgr_stream_info_t
CONFIG_INSTANCE_SetScalerAlloc_W
#define CONFIG_INSTANCE_SetScalerAlloc_W(sid, instance)
Definition: ni_nvme.h:816
CONFIG_INSTANCE_SetScalerPara_W
#define CONFIG_INSTANCE_SetScalerPara_W(sid, instance)
Definition: ni_nvme.h:811
NI_PIX_FMT_RGBA
@ NI_PIX_FMT_RGBA
Definition: ni_device_api.h:266
_ni_encoder_cfg_params::conf_win_bottom
int conf_win_bottom
Definition: ni_device_api.h:2226
_ni_metadata_enc_bstream_rev61
Definition: ni_device_api_priv.h:262
_ni_decoder_input_params_t::scale_round
int scale_round[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2438
_ni_overall_load_query::overall_instance_count
uint32_t overall_instance_count
Definition: ni_device_api.h:1180
_ni_session_context::roi_len
uint32_t roi_len
Definition: ni_device_api.h:1447
NI_DDR_PRIORITY_NONE
@ NI_DDR_PRIORITY_NONE
Definition: ni_device_api.h:1766
_ni_encoder_cfg_params::profile
int profile
Definition: ni_device_api.h:2176
ni_encoder_config_t
struct _ni_encoder_config_t ni_encoder_config_t
EP_LOG_OFFSET_IN_4K
#define EP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:661
ni_decoder_config_t
struct _ni_decoder_config_t ni_decoder_config_t
_ni_encoder_cfg_params::colorSpace
int colorSpace
Definition: ni_device_api.h:2242
_ni_session_context::max_frame_delay
int max_frame_delay
Definition: ni_device_api.h:1645
_ni_gop_params::rps
ni_gop_rps_t rps[NI_MAX_REF_PIC]
Definition: ni_device_api.h:1909
CONFIG_INSTANCE_SetEOS_W
#define CONFIG_INSTANCE_SetEOS_W(sid, instance)
Definition: ni_nvme.h:804
ni_rsrc_priv.h
Private definitions used by ni_rsrc_api.cpp for management of NETINT video processing devices.
SwapSW64
void SwapSW64(uint64_t *buf, uint64_t bytes)
Definition: ni_device_api_priv.c:211
NI_DEC_CROP_MODE_MANUAL
@ NI_DEC_CROP_MODE_MANUAL
Definition: ni_device_api.h:897
_ni_t408_config_t::pu32IntraDcDeltaRate
int32_t pu32IntraDcDeltaRate
Definition: ni_device_api_priv.h:465
NI_RETCODE_PARAM_ERROR_USR_RMD_ENC_PARAM
@ NI_RETCODE_PARAM_ERROR_USR_RMD_ENC_PARAM
Definition: ni_defs.h:466
NI_RETCODE_NVME_SC_INVALID_PARAMETER
@ NI_RETCODE_NVME_SC_INVALID_PARAMETER
Definition: ni_defs.h:537
NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_o
#define NI_FW_ENC_BITSTREAM_META_DATA_SIZE_UNDER_MAJOR_6_MINOR_o
Definition: ni_device_api_priv.h:798
_ni_encoder_cfg_params::bitrateWindow
int bitrateWindow
Definition: ni_device_api.h:2256
NI_RETCODE_ERROR_INVALID_SESSION
@ NI_RETCODE_ERROR_INVALID_SESSION
Definition: ni_defs.h:426
_ni_nvme_identity::xcoder_num_h264_decoder_hw
uint8_t xcoder_num_h264_decoder_hw
Definition: ni_nvme.h:160
_ni_session_context::pkt_offsets_index_min
uint64_t pkt_offsets_index_min[NI_FIFO_SZ]
Definition: ni_device_api.h:1403
_ni_t408_config_t::scalingListEnable
int32_t scalingListEnable
Definition: ni_device_api_priv.h:387
ni_hwupload_session_write
int ni_hwupload_session_write(ni_session_context_t *p_ctx, ni_frame_t *p_frame, niFrameSurface1_t *hwdesc)
Send a YUV p_frame to upload session.
Definition: ni_device_api_priv.c:12778
NI_PIX_FMT_ABGR
@ NI_PIX_FMT_ABGR
Definition: ni_device_api.h:269
_ni_t408_config_t::bgDetectEnable
uint32_t bgDetectEnable
Definition: ni_device_api_priv.h:443
ni_session_statistic_t
struct _ni_session_statistic_t ni_session_statistic_t
NI_MAX_ENCODER_QUERY_RETRIES
#define NI_MAX_ENCODER_QUERY_RETRIES
Definition: ni_device_api.h:66
_ni_load_query::total_pixel_load
uint32_t total_pixel_load
Definition: ni_device_api.h:1171
QUERY_GENERAL_GET_STATUS_R
#define QUERY_GENERAL_GET_STATUS_R(instance)
Definition: ni_nvme.h:781
NI_PIX_FMT_ARGB
@ NI_PIX_FMT_ARGB
Definition: ni_device_api.h:268
_ni_encoder_config_t::i32pbRatio
int32_t i32pbRatio
Definition: ni_device_api_priv.h:624
_ni_xcoder_params
Definition: ni_device_api.h:2608
TP_LOG_OFFSET_IN_4K
#define TP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:663
NI_RETRY_INTERVAL_100US
#define NI_RETRY_INTERVAL_100US
Definition: ni_device_api_priv.h:793
ni_nvme_check_error_code
ni_retcode_t ni_nvme_check_error_code(int rc, int opcode, uint32_t xcoder_type, uint32_t hw_id, uint32_t *p_instance_id)
Check f/w error return code, and if it's a fatal one, terminate application's decoding/encoding proce...
Definition: ni_nvme.c:62
ni_session_config_rw_t
struct _ni_session_config_rw ni_session_config_rw_t
_ni_metadata_enc_frame::get_recon_frame_mode
uint8_t get_recon_frame_mode
Definition: ni_device_api_priv.h:258
_ni_frame::sei_hdr_content_light_level_info_len
unsigned int sei_hdr_content_light_level_info_len
Definition: ni_device_api.h:2534
_ni_encoder_config_t::ui8enablePTS
uint8_t ui8enablePTS
Definition: ni_device_api_priv.h:522
_ni_encoder_config_t::i32userMaxDeltaQp
int32_t i32userMaxDeltaQp
Definition: ni_device_api_priv.h:515
EP_CORE
@ EP_CORE
Definition: ni_defs.h:362
SESSION_RUN_STATE_RESETTING
@ SESSION_RUN_STATE_RESETTING
Definition: ni_device_api.h:1135
NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_WIDTH_TOO_SMALL
Definition: ni_defs.h:489
_ni_encoder_config_t::i8crf
int8_t i8crf
Definition: ni_device_api_priv.h:538
_ni_metadata_enc_bstream::min_mv_x
int16_t min_mv_x[2]
Definition: ni_device_api_priv.h:288
_ni_encoder_cfg_params::max_delta_qp
int max_delta_qp
Definition: ni_device_api.h:2301
_ni_xcoder_params::staticMmapThreshold
int staticMmapThreshold
Definition: ni_device_api.h:2675
_ni_t408_config_t::tcOffsetDiv2
int32_t tcOffsetDiv2
Definition: ni_device_api_priv.h:395
_ni_encoder_cfg_params::ctbRowQpStep
int ctbRowQpStep
Definition: ni_device_api.h:2309
_ni_encoder_config_t::ui32cropWidth
uint32_t ui32cropWidth
Definition: ni_device_api_priv.h:611
_ni_encoder_config_t::ui8HDR10CLLEnable
uint8_t ui8HDR10CLLEnable
Definition: ni_device_api_priv.h:541
CONFIG_INSTANCE_SetAiPara_W
#define CONFIG_INSTANCE_SetAiPara_W(sid, instance)
Definition: ni_nvme.h:844
_ni_session_context::low_delay_sync_cond
ni_pthread_cond_t low_delay_sync_cond
Definition: ni_device_api.h:1617
_ni_frame::sei_hdr_mastering_display_color_vol_len
unsigned int sei_hdr_mastering_display_color_vol_len
Definition: ni_device_api.h:2532
NI_SESSION_CLOSE_RETRY_MAX
#define NI_SESSION_CLOSE_RETRY_MAX
Definition: ni_device_api_priv.h:775
_ni_session_context::ready_to_close
uint32_t ready_to_close
Definition: ni_device_api.h:1495
_ni_session_context::overall_load_query
ni_overall_load_query_t overall_load_query
Definition: ni_device_api.h:1454
OPEN_ADD_CODEC
#define OPEN_ADD_CODEC(instance, codec, param)
Definition: ni_nvme.h:695
_ni_encoder_config_t::i8ppsInitQp
int8_t i8ppsInitQp
Definition: ni_device_api_priv.h:627
_ni_encoder_cfg_params::decoding_refresh_type
int decoding_refresh_type
Definition: ni_device_api.h:2279
_ni_encoder_config_t::ui8picSkipEnable
uint8_t ui8picSkipEnable
Definition: ni_device_api_priv.h:562
NI_RETCODE_NVME_SC_VPU_RSRC_INSUFFICIENT
@ NI_RETCODE_NVME_SC_VPU_RSRC_INSUFFICIENT
Definition: ni_defs.h:540
_ni_encoder_config_t::ui8blockRCSize
uint8_t ui8blockRCSize
Definition: ni_device_api_priv.h:577
_ni_device_capability::xcoder_devices_cnt
uint8_t xcoder_devices_cnt
Definition: ni_device_api.h:1112
NI_ENC_MIN_RESOLUTION_HEIGHT
#define NI_ENC_MIN_RESOLUTION_HEIGHT
Definition: ni_device_api.h:89
INST_BUF_INFO_RW_READ
@ INST_BUF_INFO_RW_READ
Definition: ni_device_api_priv.h:48
ni_xcoder_session_query
int ni_xcoder_session_query(ni_session_context_t *p_ctx, ni_device_type_t device_type)
Query current xcoder status.
Definition: ni_device_api_priv.c:2991
_ni_encoder_config_t::ui32chromaLinesize
uint32_t ui32chromaLinesize
Definition: ni_device_api_priv.h:610
_ni_load_query::fw_load
uint32_t fw_load
Definition: ni_device_api.h:1154
ni_encoder_start_buffer_alloc
ni_retcode_t ni_encoder_start_buffer_alloc(ni_frame_t *p_frame)
Allocate memory for the non-4k-aligned part at the start of YUV data for encoder input data.
Definition: ni_device_api_priv.c:16826
_ni_device_capability::fw_rev
uint8_t fw_rev[8]
Definition: ni_device_api.h:1119
_ni_encoder_cfg_params::crop_width
int crop_width
Definition: ni_device_api.h:2314
GOP_PRESET_IDX_IBBBP
@ GOP_PRESET_IDX_IBBBP
Definition: ni_device_api_priv.h:321
_ni_thread_arg_struct_t
Definition: ni_device_api.h:1208
_ni_nvme_identity::fw_build_id
uint8_t fw_build_id[256]
Definition: ni_nvme.h:222
_ni_device_vf_ns_id
Definition: ni_rsrc_api.h:228
_ni_encoder_cfg_params::slice_mode
int slice_mode
Definition: ni_device_api.h:2276
XCODER_MIN_ENC_PIC_HEIGHT
#define XCODER_MIN_ENC_PIC_HEIGHT
Definition: ni_util.h:87
CPU_LOG_BUFFER_SIZE
#define CPU_LOG_BUFFER_SIZE
Definition: ni_defs.h:354
_ni_encoder_cfg_params::HDR10minluma
int HDR10minluma
Definition: ni_device_api.h:2211
ni_encoder_metadata_buffer_alloc
ni_retcode_t ni_encoder_metadata_buffer_alloc(ni_frame_t *p_frame, int extra_len)
Allocate memory for the metadata header and auxillary data for encoder input data.
Definition: ni_device_api_priv.c:16739
ni_uploader_session_close
ni_retcode_t ni_uploader_session_close(ni_session_context_t *p_ctx)
Close an xcoder upload instance.
Definition: ni_device_api_priv.c:14476
_ni_session_context::codec_format
uint32_t codec_format
Definition: ni_device_api.h:1437
_ni_device_capability
device capability type
Definition: ni_device_api.h:1108
NI_RETRY_INTERVAL_200US
#define NI_RETRY_INTERVAL_200US
Definition: ni_device_api_priv.h:792
NI_MAX_NUM_OF_DECODER_OUTPUTS
#define NI_MAX_NUM_OF_DECODER_OUTPUTS
Definition: ni_defs.h:238
_ni_metadata_enc_frame::frame_force_type
uint16_t frame_force_type
Definition: ni_device_api_priv.h:247
_ni_decoder_config_t::ui8DisableAdaptiveBuffers
uint8_t ui8DisableAdaptiveBuffers
Definition: ni_device_api_priv.h:720
_ni_encoder_config_t::ui8intraResetRefresh
uint8_t ui8intraResetRefresh
Definition: ni_device_api_priv.h:603
_ni_t408_config_t::max_num_merge
int32_t max_num_merge
Definition: ni_device_api_priv.h:391
ALL_CORE
@ ALL_CORE
Definition: ni_defs.h:360
_ni_encoder_config_t::i8hvsBaseMbComplexity
int8_t i8hvsBaseMbComplexity
Definition: ni_device_api_priv.h:631
_ni_session_context::scaler_operation
uint32_t scaler_operation
Definition: ni_device_api.h:1561
ni_posix_memalign
int ni_posix_memalign(void **memptr, size_t alignment, size_t size)
Allocate aligned memory.
Definition: ni_util.c:197
_ni_t408_config_t::intraRefreshArg
int32_t intraRefreshArg
Definition: ni_device_api_priv.h:385
_ni_session_context::stream_dir_name
char stream_dir_name[256]
Definition: ni_device_api.h:1559
_ni_encoder_config_t::ui32lumaLinesize
uint32_t ui32lumaLinesize
Definition: ni_device_api_priv.h:609
NI_RETCODE_ERROR_UNSUPPORTED_FEATURE
@ NI_RETCODE_ERROR_UNSUPPORTED_FEATURE
Definition: ni_defs.h:515
_ni_session_context::itu_t_t35_hdr10p_sei_hdr_hevc
uint8_t itu_t_t35_hdr10p_sei_hdr_hevc[NI_HDR10P_SEI_HDR_HEVC_LEN]
Definition: ni_device_api.h:1370
_ni_encoder_config_t::i8maxConsecutiveSkipFrameNum
int8_t i8maxConsecutiveSkipFrameNum
Definition: ni_device_api_priv.h:596
QUERY_INSTANCE_NL_V2_R
#define QUERY_INSTANCE_NL_V2_R(sid, instance)
Definition: ni_nvme.h:769
_ni_nvme_identity::xcoder_num_hw
uint8_t xcoder_num_hw
Definition: ni_nvme.h:159
_ni_session_context::psnr_y
double psnr_y
Definition: ni_device_api.h:1661
NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
@ NI_RETCODE_PARAM_ERROR_PIC_HEIGHT
Definition: ni_defs.h:475
_ni_encoder_cfg_params::colorPrimaries
int colorPrimaries
Definition: ni_device_api.h:2240
_ni_decoder_input_params_t::scale_wh
int scale_wh[NI_MAX_NUM_OF_DECODER_OUTPUTS][2]
Definition: ni_device_api.h:2414
CONFIG_INSTANCE_SetDecPara_W
#define CONFIG_INSTANCE_SetDecPara_W(sid, instance)
Definition: ni_nvme.h:809
_ni_t408_config_t::customLambdaEnable
uint32_t customLambdaEnable
Definition: ni_device_api_priv.h:449
ni_log2
void ni_log2(const void *p_context, ni_log_level_t level, const char *fmt,...)
print log message and additional information using ni_log_callback,
Definition: ni_log.c:337
_ni_instance_mgr_general_status::fw_video_shared_mem_usage
uint8_t fw_video_shared_mem_usage
Definition: ni_device_api_priv.h:98
NI_RETCODE_PARAM_ERROR_BRATE
@ NI_RETCODE_PARAM_ERROR_BRATE
Definition: ni_defs.h:433
_ni_encoder_cfg_params::forceBframeQpfactor
float forceBframeQpfactor
Definition: ni_device_api.h:2346
_ni_metadata_enc_frame::frame_sei_data_size
uint32_t frame_sei_data_size
Definition: ni_device_api_priv.h:244
_ni_network_layer_params_t::quant_format
int32_t quant_format
Definition: ni_device_api.h:1284
_ni_nvme_identity_xcoder_hw::hw_video_level
uint8_t hw_video_level
Definition: ni_nvme.h:84
_ni_load_query
Definition: ni_device_api.h:1150
_ni_session_context::is_auto_dl
uint8_t is_auto_dl
Definition: ni_device_api.h:1421
FP_LOG_OFFSET_IN_4K
#define FP_LOG_OFFSET_IN_4K
Definition: ni_nvme.h:664
_ni_thread_arg_struct_t::session_id
uint32_t session_id
Definition: ni_device_api.h:1211
_ni_encoder_cfg_params::crfFloat
float crfFloat
Definition: ni_device_api.h:2329
_ni_metadata_enc_bstream::recycle_index
uint32_t recycle_index
Definition: ni_device_api_priv.h:280
_ni_t408_config_t::nrIntraWeightY
uint32_t nrIntraWeightY
Definition: ni_device_api_priv.h:424
_ni_encoder_cfg_params::long_term_ref_count
int long_term_ref_count
Definition: ni_device_api.h:2220
_ni_t408_config_t::cu32InterDeltaRate
int32_t cu32InterDeltaRate
Definition: ni_device_api_priv.h:474
_ni_encoder_config_t::ui32vbvMaxRate
uint32_t ui32vbvMaxRate
Definition: ni_device_api_priv.h:626
NI_CC_SEI_HDR_HEVC_LEN
#define NI_CC_SEI_HDR_HEVC_LEN
Definition: ni_device_api.h:448
ni_nvme.h
Private definitions for interfacing with NETINT video processing devices over NVMe.
_ni_t408_config_t::custom_gop_params
ni_custom_gop_params_t custom_gop_params
Definition: ni_device_api_priv.h:407
ni_query_detail_status
int ni_query_detail_status(ni_session_context_t *p_ctx, ni_device_type_t device_type, void *p_detail_status, int ver)
Query a particular xcoder instance to get DetailStatus data.
Definition: ni_device_api_priv.c:5831
_niFrameSurface1::ui16width
uint16_t ui16width
Definition: ni_device_api.h:2691
_ni_session_context::flags_array
int flags_array[NI_FIFO_SZ]
Definition: ni_device_api.h:1595
_ni_instance_mgr_allocation_info::picture_width
uint16_t picture_width
Definition: ni_device_api_priv.h:157
ni_scaler_params_t
struct _ni_scaler_params_t ni_scaler_params_t
QUERY_INSTANCE_NL_R
#define QUERY_INSTANCE_NL_R(sid, instance)
Definition: ni_nvme.h:757
_ni_metadata_enc_bstream::max_mv_y
int16_t max_mv_y[2]
Definition: ni_device_api_priv.h:289
_ni_thread_arg_struct_t::thread_event_handle
ni_event_handle_t thread_event_handle
Definition: ni_device_api.h:1216
_ni_instance_mgr_stream_info::transfer_frame_stride
uint16_t transfer_frame_stride
Definition: ni_device_api_priv.h:126
_ni_encoder_config_t::i32ltrRefQpOffset
int32_t i32ltrRefQpOffset
Definition: ni_device_api_priv.h:564
_ni_session_context::load_query
ni_load_query_t load_query
Definition: ni_device_api.h:1451
_ni_session_statistic_t::ui32FramesDropped
uint32_t ui32FramesDropped
Definition: ni_defs.h:378
_ni_session_data_io::frame
ni_frame_t frame
Definition: ni_device_api.h:2754
_ni_encoder_config_t::ui16hdr10_dx0
uint16_t ui16hdr10_dx0
Definition: ni_device_api_priv.h:585
_ni_t408_config_t::initialRcQp
int32_t initialRcQp
Definition: ni_device_api_priv.h:417
_ni_metadata_enc_bstream::frame_tstamp
uint64_t frame_tstamp
Definition: ni_device_api_priv.h:277
_ni_decoder_input_params_t::enable_out2
int enable_out2
Definition: ni_device_api.h:2404
_ni_encoder_config_t::ui8rdoLevel
uint8_t ui8rdoLevel
Definition: ni_device_api_priv.h:537
_ni_encoder_cfg_params::level_idc
int level_idc
Definition: ni_device_api.h:2177
_ni_scaler_params_t::filterblit
int filterblit
Definition: ni_device_api.h:2467
_ni_encoder_cfg_params::HDR10dy2
int HDR10dy2
Definition: ni_device_api.h:2207
INST_BUF_INFO_RW_READ_BUSY
@ INST_BUF_INFO_RW_READ_BUSY
Definition: ni_device_api_priv.h:51
_ni_instance_mgr_allocation_info
Definition: ni_device_api_priv.h:155
_ni_t408_config_t::intra_mb_refresh_mode
int32_t intra_mb_refresh_mode
Definition: ni_device_api_priv.h:491
NI_NUM_OF_PIXELS_1440P
#define NI_NUM_OF_PIXELS_1440P
Definition: ni_device_api.h:78
_ni_uploader_config_t::ui16picHeight
uint16_t ui16picHeight
Definition: ni_device_api_priv.h:644
_niFrameSurface1::output_idx
int8_t output_idx
Definition: ni_device_api.h:2697
NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
@ NI_H265_USER_DATA_FLAG_CONTENT_LIGHT_LEVEL_INFO
Definition: ni_device_api.h:362
NI_SCALER_FLAG_P2
#define NI_SCALER_FLAG_P2
Definition: ni_device_api.h:285
_ni_thread_arg_struct_t::session_timestamp
uint64_t session_timestamp
Definition: ni_device_api.h:1212
_ni_nvme_identity_xcoder_hw::hw_codec_format
uint8_t hw_codec_format
Definition: ni_nvme.h:77
_ni_encoder_config_t::ui8hdr10_enable
uint8_t ui8hdr10_enable
Definition: ni_device_api_priv.h:584
_ni_session_context::pts_table
ni_timestamp_table_t * pts_table
Definition: ni_device_api.h:1466
_ni_metadata_enc_frame::frame_force_type_enable
uint16_t frame_force_type_enable
Definition: ni_device_api_priv.h:246
_ni_metadata_enc_bstream::min_mv_y
int16_t min_mv_y[2]
Definition: ni_device_api_priv.h:290
COMPILE_ASSERT
#define COMPILE_ASSERT(condition)
Definition: ni_util.h:76
NI_MAX_NUM_SW_FRAME_DATA_POINTERS
#define NI_MAX_NUM_SW_FRAME_DATA_POINTERS
Definition: ni_defs.h:229
CONFIG_SESSION_FRAME_COPY_W
#define CONFIG_SESSION_FRAME_COPY_W(sid)
Definition: ni_nvme.h:896
NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
@ NI_RETCODE_PARAM_ERROR_CUSTOM_GOP
Definition: ni_defs.h:471
_ni_frameclone_desc::ui32Size
uint32_t ui32Size
Definition: ni_device_api.h:1331
_ni_t408_config_t::timeScale
uint32_t timeScale
Definition: ni_device_api_priv.h:411
_ni_encoder_config_t::ui16hdr10_dy0
uint16_t ui16hdr10_dy0
Definition: ni_device_api_priv.h:586
_ni_encoder_cfg_params::maxFrameSize
int maxFrameSize
Definition: ni_device_api.h:2171
_ni_encoder_cfg_params::hvs_qp_scale
int hvs_qp_scale
Definition: ni_device_api.h:2294
_ni_metadata_enc_bstream::intra_total_count
uint16_t intra_total_count
Definition: ni_device_api_priv.h:293
_ni_encoder_cfg_params::cplxDecay
float cplxDecay
Definition: ni_device_api.h:2325
_ni_packet::av1_show_frame
int av1_show_frame
Definition: ni_device_api.h:2743
_ni_instance_mgr_general_status
Definition: ni_device_api_priv.h:79
atoi
#define atoi(p_str)
Definition: ni_device_api.c:6834
_ni_t408_config_t::nrYEnable
uint32_t nrYEnable
Definition: ni_device_api_priv.h:419
_ni_uploader_config_t::ui16picWidth
uint16_t ui16picWidth
Definition: ni_device_api_priv.h:643
ni_get_hw_yuv420p_dim
void ni_get_hw_yuv420p_dim(int width, int height, int factor, int is_semiplanar, int plane_stride[NI_MAX_NUM_DATA_POINTERS], int plane_height[NI_MAX_NUM_DATA_POINTERS])
Get dimension information of Netint HW YUV420p frame to be sent to encoder for encoding....
Definition: ni_util.c:2022
_ni_frame::sei_user_data_unreg_offset
unsigned int sei_user_data_unreg_offset
Definition: ni_device_api.h:2539
NI_PARAM_MAX_WIDTH
#define NI_PARAM_MAX_WIDTH
Definition: ni_device_api.h:135
_ni_t408_config_t::bitAllocMode
int32_t bitAllocMode
Definition: ni_device_api_priv.h:399
END
#define END
Definition: ni_defs.h:319
_ni_encoder_cfg_params::enable_hvs_qp
int enable_hvs_qp
Definition: ni_device_api.h:2293
_ni_t408_config_t::bgDeltaQp
int32_t bgDeltaQp
Definition: ni_device_api_priv.h:447
_ni_frame::video_height
uint32_t video_height
Definition: ni_device_api.h:2507
ni_config_instance_set_decoder_params
ni_retcode_t ni_config_instance_set_decoder_params(ni_session_context_t *p_ctx, uint32_t max_pkt_size)
Send a p_config command to configure decoding parameters.
Definition: ni_device_api_priv.c:14606
_ni_t408_config_t::pu08IntraDcDeltaRate
int32_t pu08IntraDcDeltaRate
Definition: ni_device_api_priv.h:459
_ni_network_data::inset
ni_network_layer_offset_t * inset
Definition: ni_device_api.h:1320
_ni_decoder_input_params_t::force_8_bit
int force_8_bit[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2407
_ni_t408_config_t::cu_size_mode
int32_t cu_size_mode
Definition: ni_device_api_priv.h:388
_ni_encoder_cfg_params::qcomp
float qcomp
Definition: ni_device_api.h:2319
_ni_session_context::low_delay_sync_flag
int low_delay_sync_flag
Definition: ni_device_api.h:1615
_ni_packet::pos
long long pos
Definition: ni_device_api.h:2723
_ni_t408_config_t::lfCrossSliceBoundaryEnable
int32_t lfCrossSliceBoundaryEnable
Definition: ni_device_api_priv.h:393
_ni_frame_config::output_index
uint8_t output_index
Definition: ni_device_api.h:2715
_ni_network_buffer::ui16Width
uint16_t ui16Width
Definition: ni_device_api_priv.h:735
_ni_encoder_config_t::ui8sliceMode
uint8_t ui8sliceMode
Definition: ni_device_api_priv.h:509
NI_CC_SEI_BYTE4
#define NI_CC_SEI_BYTE4
Definition: ni_device_api.h:435
_ni_input_frame::p_input_buffer
uint8_t * p_input_buffer
Definition: ni_device_api.h:1349
_ni_xcoder_params::ddr_priority_mode
ni_ddr_priority_mode_t ddr_priority_mode
Definition: ni_device_api.h:2682
_ni_encoder_cfg_params::colorTrc
int colorTrc
Definition: ni_device_api.h:2241
ni_encoder_session_send_eos
ni_retcode_t ni_encoder_session_send_eos(ni_session_context_t *p_ctx)
Flush encoder output.
Definition: ni_device_api_priv.c:3635
_ni_session_context::active_video_height
uint32_t active_video_height
Definition: ni_device_api.h:1502
_ni_encoder_config_t::ui8gopLowdelay
uint8_t ui8gopLowdelay
Definition: ni_device_api_priv.h:547
ni_dump_log_all_cores
ni_retcode_t ni_dump_log_all_cores(ni_session_context_t *p_ctx, void *p_data, bool gen_log_file)
Definition: ni_device_api_priv.c:17142
NI_MAX_SEI_DATA
#define NI_MAX_SEI_DATA
Definition: ni_device_api.h:414
_ni_session_context::pkt_delay_cnt
uint32_t pkt_delay_cnt
Definition: ni_device_api.h:1653
ni_queue_free
ni_retcode_t ni_queue_free(ni_queue_t *p_queue, ni_queue_buffer_pool_t *p_buffer_pool)
Free xcoder queue.
Definition: ni_util.c:1793
_ni_t408_config_t::intra_qp
int32_t intra_qp
Definition: ni_device_api_priv.h:374
_ni_encoder_cfg_params::av1_error_resilient_mode
int av1_error_resilient_mode
Definition: ni_device_api.h:2307
NI_CC_SEI_BYTE6
#define NI_CC_SEI_BYTE6
Definition: ni_device_api.h:437
_ni_session_context::enc_pts_r_idx
int64_t enc_pts_r_idx
Definition: ni_device_api.h:1391
_ni_encoder_cfg_params::tune_bframe_visual
int tune_bframe_visual
Definition: ni_device_api.h:2347
ni_session_keep_alive_thread
void * ni_session_keep_alive_thread(void *arguments)
decoder keep alive thread function triggers every 1 second
Definition: ni_device_api_priv.c:12327
_ni_encoder_cfg_params::rc
struct _ni_encoder_cfg_params::@13 rc
ni_instance_mgr_stream_complete_t
struct _ni_instance_mgr_stream_complete ni_instance_mgr_stream_complete_t
_ni_network_buffer
Definition: ni_device_api_priv.h:732
_ni_encoder_cfg_params::conf_win_left
int conf_win_left
Definition: ni_device_api.h:2227
NI_MAX_FRAME_SIZE
#define NI_MAX_FRAME_SIZE
Definition: ni_device_api.h:178
_ni_encoder_cfg_params::pass1_qp
int pass1_qp
Definition: ni_device_api.h:2328
_ni_session_context::rc_error_count
int rc_error_count
Definition: ni_device_api.h:1484
_ni_encoder_cfg_params::conf_win_right
int conf_win_right
Definition: ni_device_api.h:2228
_ni_encoder_config_t::ui8rcQpDeltaRange
uint8_t ui8rcQpDeltaRange
Definition: ni_device_api_priv.h:579
CONFIG_INSTANCE_UploadModel_W
#define CONFIG_INSTANCE_UploadModel_W(sid, instance)
Definition: ni_nvme.h:902
_ni_session_context::blk_dev_name
char blk_dev_name[NI_MAX_DEVICE_NAME_LEN]
Definition: ni_device_api.h:1610
_ni_decoder_input_params_t::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:2422
NI_POOL_TYPE_NONE
@ NI_POOL_TYPE_NONE
Definition: ni_device_api.h:470
_ni_session_context::biggest_bitstream_buffer_allocated
uint32_t biggest_bitstream_buffer_allocated
Definition: ni_device_api.h:1506
_ni_session_context::session_run_state
ni_session_run_state_t session_run_state
Definition: ni_device_api.h:1498
NI_NVME_IDENTITY_CMD_DATA_SZ
#define NI_NVME_IDENTITY_CMD_DATA_SZ
Definition: ni_nvme.h:38
ni_query_stream_info
ni_retcode_t ni_query_stream_info(ni_session_context_t *p_ctx, ni_device_type_t device_type, ni_instance_mgr_stream_info_t *p_stream_info)
Query a particular xcoder instance to get Stream Info data.
Definition: ni_device_api_priv.c:5913
_ni_session_context::enable_low_delay_check
int enable_low_delay_check
Definition: ni_device_api.h:1546
_ni_t408_config_t::lambdaScalingEnable
int32_t lambdaScalingEnable
Definition: ni_device_api_priv.h:487
_ni_session_context::sent_size
uint32_t sent_size
Definition: ni_device_api.h:1459
_niFrameSurface1::device_handle
int32_t device_handle
Definition: ni_device_api.h:2694
_ni_encoder_cfg_params::crfMax
int crfMax
Definition: ni_device_api.h:2318
NI_CC_SEI_TRAILER_LEN
#define NI_CC_SEI_TRAILER_LEN
Definition: ni_device_api.h:452
_ni_encoder_config_t::ui8bitrateMode
uint8_t ui8bitrateMode
Definition: ni_device_api_priv.h:628
_ni_metadata_common::frame_height
uint16_t frame_height
Definition: ni_device_api_priv.h:219
_niFrameSurface1::bit_depth
int8_t bit_depth
Definition: ni_device_api.h:2695
g_dev_handle
ni_device_handle_t g_dev_handle
Definition: ni_rsrc_api.cpp:63
_ni_t408_config_t::weightPredEnable
uint32_t weightPredEnable
Definition: ni_device_api_priv.h:442
_ni_decoder_config_t::fps_denominator
uint32_t fps_denominator
Definition: ni_device_api_priv.h:711
_ni_metadata_enc_bstream::frameCropLeftOffset
uint16_t frameCropLeftOffset
Definition: ni_device_api_priv.h:302
_ni_metadata_enc_bstream_rev61::av1_show_frame
uint32_t av1_show_frame
Definition: ni_device_api_priv.h:270
_ni_p2p_sgl_t::ui32DMALen
uint32_t ui32DMALen[NI_MAX_P2P_SGL_ENTRY]
Definition: ni_defs.h:399
_ni_t408_config_t::nrInterWeightCb
uint32_t nrInterWeightCb
Definition: ni_device_api_priv.h:428
_ni_encoder_cfg_params::cuTreeFactor
int cuTreeFactor
Definition: ni_device_api.h:2322
_ni_network_layer_info::out_param
ni_network_layer_params_t * out_param
Definition: ni_device_api.h:1306
_ni_encoder_cfg_params::enableipRatio
int enableipRatio
Definition: ni_device_api.h:2339
NI_CODEC_HW_PAYLOAD_OFFSET
@ NI_CODEC_HW_PAYLOAD_OFFSET
Definition: ni_device_api.h:911
_ni_decoder_input_params_t::scale_resolution_ceil
int scale_resolution_ceil[NI_MAX_NUM_OF_DECODER_OUTPUTS]
Definition: ni_device_api.h:2437
ni_device_api.h
Public definitions for operating NETINT video processing devices for video processing.
_ni_device_capability::serial_number
uint8_t serial_number[20]
Definition: ni_device_api.h:1116
_ni_t408_config_t::rdoSkip
int32_t rdoSkip
Definition: ni_device_api_priv.h:486
NI_MAX_RESOLUTION_RGBA_HEIGHT
#define NI_MAX_RESOLUTION_RGBA_HEIGHT
Definition: ni_device_api.h:104
_ni_instance_upload_ret_hwdesc::frame_index
int16_t frame_index
Definition: ni_device_api_priv.h:184
_ni_session_context::meta_size
uint32_t meta_size
Params used in VFR mode Done///.
Definition: ni_device_api.h:1589
_ni_encoder_cfg_params::vbv_min_rate
int vbv_min_rate
Definition: ni_device_api.h:2343
NI_DEVICE_TYPE_XCODER_MAX
@ NI_DEVICE_TYPE_XCODER_MAX
Definition: ni_defs.h:345
CONFIG_INSTANCE_SetSOS_W
#define CONFIG_INSTANCE_SetSOS_W(sid, instance)
Definition: ni_nvme.h:802
_ni_t408_config_t::pu04IntraDcDeltaRate
int32_t pu04IntraDcDeltaRate
Definition: ni_device_api_priv.h:456
ni_get_core_name
char * ni_get_core_name(ni_core_type_t eCoreType)
Definition: ni_device_api_priv.c:16966
_ni_encoder_cfg_params::HDR10MaxLight
int HDR10MaxLight
Definition: ni_device_api.h:2198
_ni_encoder_config_t::ui8ctbRcMode
uint8_t ui8ctbRcMode
Definition: ni_device_api_priv.h:544
_ni_xcoder_params::cacheRoi
int cacheRoi
Definition: ni_device_api.h:2642
_ni_nvme_identity_xcoder_hw::hw_id
uint8_t hw_id
Definition: ni_nvme.h:74
_ni_session_context::session_timestamp
uint64_t session_timestamp
Definition: ni_device_api.h:1433
_ni_session_context::psnr_u
double psnr_u
Definition: ni_device_api.h:1662
_ni_dec_mastering_display_colour_volume_bytes::white_point_x
uint16_t white_point_x
Definition: ni_device_api.h:1031
_ni_encoder_cfg_params::crop_height
int crop_height
Definition: ni_device_api.h:2315
_ni_hw_capability::codec_format
uint8_t codec_format
Definition: ni_device_api.h:1094
_ni_frame_config::rectangle_width
uint16_t rectangle_width
Definition: ni_device_api.h:2708
_ni_session_context::max_nvme_io_size
uint32_t max_nvme_io_size
Definition: ni_device_api.h:1427
_ni_session_context::enable_user_data_sei_passthru
int enable_user_data_sei_passthru
Definition: ni_device_api.h:1565
_ni_encoder_config_t::ui8videoFullRange
uint8_t ui8videoFullRange
Definition: ni_device_api_priv.h:568
_ni_encoder_cfg_params::aspectRatioHeight
int aspectRatioHeight
Definition: ni_device_api.h:2169
ni_config_instance_set_encoder_frame_params
ni_retcode_t ni_config_instance_set_encoder_frame_params(ni_session_context_t *p_ctx, ni_encoder_frame_params_t *p_params)
Send a p_config command to configure encoding p_frame parameters.
Definition: ni_device_api_priv.c:7100
NI_RETCODE_EAGAIN
@ NI_RETCODE_EAGAIN
Definition: ni_defs.h:522
_ni_timestamp_table_t
Definition: ni_device_api.h:1272
_ni_dec_mastering_display_colour_volume_bytes::max_display_mastering_luminance
uint32_t max_display_mastering_luminance
Definition: ni_device_api.h:1033
NI_MIN_HEIGHT
#define NI_MIN_HEIGHT
Definition: ni_device_api.h:126
_ni_overall_load_query::admin_queried
uint32_t admin_queried
Definition: ni_device_api.h:1181
_ni_t408_config_t::minQpI
int32_t minQpI
Definition: ni_device_api_priv.h:477
NI_RETCODE_FAILURE
@ NI_RETCODE_FAILURE
Definition: ni_defs.h:421
_ni_encoder_cfg_params::HDR10dx1
int HDR10dx1
Definition: ni_device_api.h:2204
_ni_nvme_identity::hw2_codec_format
uint8_t hw2_codec_format
Definition: ni_nvme.h:195
_ni_encoder_config_t::ui32VuiDataSizeBytes
uint32_t ui32VuiDataSizeBytes
Definition: ni_device_api_priv.h:533
_ni_session_context::is_first_frame
int is_first_frame
Definition: ni_device_api.h:1387
_ni_t408_config_t::tmvpEnable
int32_t tmvpEnable
Definition: ni_device_api_priv.h:389
NI_HDR10P_SEI_BYTE4
#define NI_HDR10P_SEI_BYTE4
Definition: ni_device_api.h:443
_ni_encoder_config_t::ui8enableSSIM
uint8_t ui8enableSSIM
Definition: ni_device_api_priv.h:583
ni_ai_alloc_hwframe
ni_retcode_t ni_ai_alloc_hwframe(ni_session_context_t *p_ctx, int width, int height, int options, int pool_size, int frame_index)
Definition: ni_device_api_priv.c:16305
_ni_instance_mgr_general_status::fw_p2p_mem_usage
uint8_t fw_p2p_mem_usage
Definition: ni_device_api_priv.h:95
_ni_hw_capability::max_video_width
uint16_t max_video_width
Definition: ni_device_api.h:1096
_ni_decoder_config_t::ui32SourceWidth
uint32_t ui32SourceWidth
Definition: ni_device_api_priv.h:722
_ni_encoder_config_t::ui16maxFrameSize
uint16_t ui16maxFrameSize
Definition: ni_device_api_priv.h:582
_ni_encoder_config_t::ui32horOffset
uint32_t ui32horOffset
Definition: ni_device_api_priv.h:613
NI_MIN_BITRATE
#define NI_MIN_BITRATE
Definition: ni_device_api.h:114
_ni_encoder_cfg_params::planar
int planar
Definition: ni_device_api.h:2170
_ni_instance_mgr_general_status::admin_nsid
uint8_t admin_nsid
Definition: ni_device_api_priv.h:105
_ni_frame::start_of_stream
uint32_t start_of_stream
Definition: ni_device_api.h:2505
_ni_packet::buffer_size
uint32_t buffer_size
Definition: ni_device_api.h:2736
_ni_t408_config_t::decoding_refresh_type
int32_t decoding_refresh_type
Definition: ni_device_api_priv.h:373
_ni_instance_mgr_stream_info::picture_width
uint16_t picture_width
Definition: ni_device_api_priv.h:124
NI_RETCODE_PARAM_ERROR_GOP_PRESET
@ NI_RETCODE_PARAM_ERROR_GOP_PRESET
Definition: ni_defs.h:441
_ni_instance_mgr_general_status::fw_load
uint8_t fw_load
Definition: ni_device_api_priv.h:97
NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_HEIGHT_TOO_SMALL
Definition: ni_defs.h:493
_ni_sei_header::type
uint8_t type
Definition: ni_device_api.h:334
_ni_session_context::dts_queue
ni_timestamp_table_t * dts_queue
Definition: ni_device_api.h:1469
_ni_encoder_frame_params::video_height
uint16_t video_height
Definition: ni_device_api_priv.h:202
_ni_frame::pts
long long pts
Definition: ni_device_api.h:2503
_ni_nvme_identity::hw2_video_level
uint8_t hw2_video_level
Definition: ni_nvme.h:202
_ni_xcoder_params::enable_vfr
int enable_vfr
Definition: ni_device_api.h:2673
_ni_decoder_config_t::ui8EnablelowDelayCheck
uint8_t ui8EnablelowDelayCheck
Definition: ni_device_api_priv.h:714
_ni_frame::inconsecutive_transfer
uint8_t inconsecutive_transfer
Definition: ni_device_api.h:2604
_ni_overall_load_query::overall_current_load
uint32_t overall_current_load
Definition: ni_device_api.h:1178
NI_RETCODE_PARAM_ERROR_FRATE
@ NI_RETCODE_PARAM_ERROR_FRATE
Definition: ni_defs.h:431
_ni_frame::crop_right
uint32_t crop_right
Definition: ni_device_api.h:2512
_ni_nvme_identity::hw1_video_level
uint8_t hw1_video_level
Definition: ni_nvme.h:189
_ni_session_context::last_framerate
ni_framerate_t last_framerate
Definition: ni_device_api.h:1626
NI_SCALER_FLAG_PC
#define NI_SCALER_FLAG_PC
Definition: ni_device_api.h:283
NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
@ NI_RETCODE_PARAM_ERROR_CU_SIZE_MODE
Definition: ni_defs.h:443
_ni_encoder_config_t::ui16HDR10MaxLight
uint16_t ui16HDR10MaxLight
Definition: ni_device_api_priv.h:539
_ni_encoder_cfg_params::rdoLevel
int rdoLevel
Definition: ni_device_api.h:2196
_ni_t408_config_t::slice_mode
int32_t slice_mode
Definition: ni_device_api_priv.h:489
_ni_encoder_cfg_params::long_term_ref_enable
int long_term_ref_enable
Definition: ni_device_api.h:2187
_ni_frame::pixel_format
int pixel_format
Definition: ni_device_api.h:2568
_ni_encoder_config_t::i32frameRateDenominator
int32_t i32frameRateDenominator
Definition: ni_device_api_priv.h:559
_ni_encoder_config_t::i32picWidth
int32_t i32picWidth
Definition: ni_device_api_priv.h:506
NI_T35_SEI_CLOSED_CAPTION
@ NI_T35_SEI_CLOSED_CAPTION
Definition: ni_device_api_priv.c:88
_ni_nvme_identity::ui16Ssvid
uint16_t ui16Ssvid
Definition: ni_nvme.h:94
NI_RETCODE_PARAM_ERROR_TOO_SMALL
@ NI_RETCODE_PARAM_ERROR_TOO_SMALL
Definition: ni_defs.h:483
_ni_encoder_cfg_params::get_psnr_mode
int get_psnr_mode
Definition: ni_device_api.h:2349
QUERY_INSTANCE_STREAM_INFO_R
#define QUERY_INSTANCE_STREAM_INFO_R(sid, instance)
Definition: ni_nvme.h:718
_ni_encoder_config_t::i32userQpMax
int32_t i32userQpMax
Definition: ni_device_api_priv.h:512
ni_util.h
Utility definitions.
hwdl_frame
int hwdl_frame(ni_session_context_t *p_ctx, ni_session_data_io_t *p_session_data, ni_frame_t *p_src_frame, int output_format)
Download hw frames by HwDesc.
Definition: ni_device_test.c:3332
_ni_session_context::av1_pkt_num
uint64_t av1_pkt_num
Definition: ni_device_api.h:1646
ni_decoder_output_config_t::ui8EnablePpuScaleAdapt
uint8_t ui8EnablePpuScaleAdapt
Definition: ni_device_api_priv.h:698
_ni_encoder_cfg_params::ltrNextInterval
int ltrNextInterval
Definition: ni_device_api.h:2216
_ni_encoder_cfg_params::videoFullRange
int videoFullRange
Definition: ni_device_api.h:2218
ni_network_perf_metrics_t
struct _ni_network_perf_metrics ni_network_perf_metrics_t
_ni_session_context::last_pts
int64_t last_pts
Definition: ni_device_api.h:1388
ni_device_session_hwdl
int ni_device_session_hwdl(ni_session_context_t *p_ctx, ni_session_data_io_t *p_data, niFrameSurface1_t *hwdesc)
Reads YUV data from hw descriptor stored location on device.
Definition: ni_device_api.c:7865
ni_get_memory_offset
ni_retcode_t ni_get_memory_offset(ni_session_context_t *p_ctx, const niFrameSurface1_t *hwdesc, uint32_t *p_offset)
Get an address offset from a hw descriptor.
Definition: ni_device_api_priv.c:14902
_ni_encoder_cfg_params::intra_reset_refresh
int intra_reset_refresh
Definition: ni_device_api.h:2308
_ni_uploader_config_t::ui8poolSize
uint8_t ui8poolSize
Definition: ni_device_api_priv.h:645
SESSION_WRITE_CONFIG
@ SESSION_WRITE_CONFIG
Definition: ni_device_api_priv.h:43
ni_decode_cropping_rectangle::ui16Y
uint16_t ui16Y
Definition: ni_device_api_priv.h:678
INST_BUF_INFO_RW_UPLOAD
@ INST_BUF_INFO_RW_UPLOAD
Definition: ni_device_api_priv.h:50
ni_device_set_ddr_configuration
ni_retcode_t ni_device_set_ddr_configuration(ni_session_context_t *p_ctx, uint8_t ddr_priority_mode)
Set DDR configuration of Quadra device.
Definition: ni_device_api_priv.c:16676
NI_PIX_FMT_10_TILED4X4
@ NI_PIX_FMT_10_TILED4X4
Definition: ni_device_api.h:276
_ni_encoder_cfg_params::slice_arg
int slice_arg
Definition: ni_device_api.h:2277
NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
@ NI_H265_USERDATA_FLAG_UNREGISTERED_SUF
Definition: ni_device_api.h:355
_ni_frame::p_metadata_buffer
uint8_t * p_metadata_buffer
Definition: ni_device_api.h:2591
SESSION_READ_CONFIG
@ SESSION_READ_CONFIG
Definition: ni_device_api_priv.h:42
_ni_encoder_config_t::ui16hdr10_wy
uint16_t ui16hdr10_wy
Definition: ni_device_api_priv.h:592
_ni_encoder_config_t::i16ctbRowQpStep
int16_t i16ctbRowQpStep
Definition: ni_device_api_priv.h:604
WRITE_METADATA_W
#define WRITE_METADATA_W(sid, instance)
Definition: ni_nvme.h:675
lower_pixel_rate
int lower_pixel_rate(const ni_load_query_t *pQuery, uint32_t ui32CurrentLowest)
Definition: ni_device_api_priv.c:17284
NI_GET_MIN_HWDESC_P2P_BUF_ID
#define NI_GET_MIN_HWDESC_P2P_BUF_ID(x)
Definition: ni_defs.h:265
_ni_nvme_identity::xcoder_num_elements
uint8_t xcoder_num_elements
Definition: ni_nvme.h:229
_ni_t408_config_t::nrNoiseSigmaCr
uint32_t nrNoiseSigmaCr
Definition: ni_device_api_priv.h:434
ni_frame_buffer_alloc_dl
ni_retcode_t ni_frame_buffer_alloc_dl(ni_frame_t *p_frame, int video_width, int video_height, int pixel_format)
Allocate preliminary memory for the frame buffer based on provided parameters.
Definition: ni_device_api.c:2323
_ni_encoder_cfg_params::lookAheadDepth
int lookAheadDepth
Definition: ni_device_api.h:2195
NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
#define NI_MAX_AI_NETWORK_BINARY_BUFFER_QUERY_RETRIES
Definition: ni_device_api_priv.h:790
NI_RETCODE_ERROR_INVALID_HANDLE
@ NI_RETCODE_ERROR_INVALID_HANDLE
Definition: ni_defs.h:504
NI_RETCODE_PARAM_ERROR_INTRA_QP
@ NI_RETCODE_PARAM_ERROR_INTRA_QP
Definition: ni_defs.h:440
ni_populate_device_capability_struct
void ni_populate_device_capability_struct(ni_device_capability_t *p_cap, void *p_data)
Get info from received xcoder capability.
Definition: ni_device_api_priv.c:7619
_ni_frame_config::rectangle_y
int16_t rectangle_y
Definition: ni_device_api.h:2711
NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
@ NI_RETCODE_NVME_SC_WRITE_BUFFER_FULL
Definition: ni_defs.h:527
_ni_t408_config_t::independSliceMode
int32_t independSliceMode
Definition: ni_device_api_priv.h:380
ni_dump_log_single_core
ni_retcode_t ni_dump_log_single_core(ni_session_context_t *p_ctx, void *p_data, uint32_t core_id, bool gen_log_file)
Definition: ni_device_api_priv.c:17025
_ni_custom_gop_params::pic_param
ni_gop_params_t pic_param[NI_MAX_GOP_NUM]
Definition: ni_device_api.h:1995
_ni_session_context::pic_reorder_delay
int pic_reorder_delay
Definition: ni_device_api.h:1592
ni_encoder_session_read
int ni_encoder_session_read(ni_session_context_t *p_ctx, ni_packet_t *p_packet)
Definition: ni_device_api_priv.c:4461
_ni_instance_mgr_general_status::active_sub_instances_cnt
uint8_t active_sub_instances_cnt
Definition: ni_device_api_priv.h:81
ni_aligned_free
#define ni_aligned_free(p_memptr)
Definition: ni_util.h:356
_ni_session_context::dev
unsigned short dev
Definition: ni_device_api.h:1572
_ni_xcoder_params::low_delay_mode
int low_delay_mode
Definition: ni_device_api.h:2628
_ni_decoder_input_params_t::decoder_low_delay
int decoder_low_delay
Definition: ni_device_api.h:2420
_ni_metadata_enc_bstream::av1_show_frame
uint32_t av1_show_frame
Definition: ni_device_api_priv.h:281
_ni_metadata_enc_bstream::reconChromaWidth
uint16_t reconChromaWidth
Definition: ni_device_api_priv.h:300
_ni_decoder_input_params_t::nb_save_pkt
int nb_save_pkt
Definition: ni_device_api.h:2406
_ni_device_vf_ns_id::vf_id
uint16_t vf_id
Definition: ni_rsrc_api.h:230
OPT_1
@ OPT_1
Definition: ni_device_api_priv.c:94
_ni_packet::video_width
uint32_t video_width
Definition: ni_device_api.h:2727
NI_MIN_RESOLUTION_HEIGHT
#define NI_MIN_RESOLUTION_HEIGHT
Definition: ni_device_api.h:87
QUERY_INSTANCE_METRICS_R
#define QUERY_INSTANCE_METRICS_R(sid, instance)
Definition: ni_nvme.h:775
_ni_encoder_config_t::ui8colorDescPresent
uint8_t ui8colorDescPresent
Definition: ni_device_api_priv.h:550
_ni_metadata_enc_frame::frame_roi_avg_qp
uint32_t frame_roi_avg_qp
Definition: ni_device_api_priv.h:242
_ni_session_context::pkt_num
uint64_t pkt_num
Definition: ni_device_api.h:1483
_ni_encoder_cfg_params::enable_cu_level_rate_control
int enable_cu_level_rate_control
Definition: ni_device_api.h:2292
_ni_decoder_input_params_t::disable_adaptive_buffers
int disable_adaptive_buffers
Definition: ni_device_api.h:2440
NI_SCALER_OPCODE_ROTATE
@ NI_SCALER_OPCODE_ROTATE
Definition: ni_defs.h:571
NI_CODEC_FORMAT_H264
@ NI_CODEC_FORMAT_H264
Definition: ni_device_api.h:878
_ni_frame::use_cur_src_as_long_term_pic
uint8_t use_cur_src_as_long_term_pic
Definition: ni_device_api.h:2517
INST_BUF_INFO_RW_WRITE
@ INST_BUF_INFO_RW_WRITE
Definition: ni_device_api_priv.h:49
ni_ai_session_read_hwdesc
ni_retcode_t ni_ai_session_read_hwdesc(ni_session_context_t *p_ctx, ni_frame_t *p_frame)
read a hardware descriptor from a scaler session
Definition: ni_device_api_priv.c:16499
_ni_session_stats::ui16ErrorCount
uint16_t ui16ErrorCount
Definition: ni_device_api_priv.h:138
_ni_thread_arg_struct_t::plast_access_time
volatile uint64_t * plast_access_time
Definition: ni_device_api.h:1220
_ni_frame::start_buffer_size
uint32_t start_buffer_size
Definition: ni_device_api.h:2599
_ni_decoder_config_t::ui8UduSeiEnabled
uint8_t ui8UduSeiEnabled
Definition: ni_device_api_priv.h:708
_ni_t408_config_t::independSliceModeArg
int32_t independSliceModeArg
Definition: ni_device_api_priv.h:381
NI_MAX_BITRATE
#define NI_MAX_BITRATE
Definition: ni_device_api.h:113
NI_RETCODE_PARAM_ERROR_OOR
@ NI_RETCODE_PARAM_ERROR_OOR
Definition: ni_defs.h:486
ni_log_get_utime
uint64_t ni_log_get_utime()
Get time for logs with microsecond timestamps.
Definition: ni_log.c:308
_ni_frame::video_width
uint32_t video_width
Definition: ni_device_api.h:2506
_ni_t408_config_t::pu32DeltaRate
int32_t pu32DeltaRate
Definition: ni_device_api_priv.h:454
NI_QUADRA_MEMORY_CONFIG_SR
#define NI_QUADRA_MEMORY_CONFIG_SR
Definition: ni_device_api_priv.h:782
_ni_session_context::initial_frame_delay
int initial_frame_delay
Definition: ni_device_api.h:1643
_ni_xcoder_params::bitrate
int bitrate
Definition: ni_device_api.h:2624
_ni_encoder_cfg_params::HDR10wy
int HDR10wy
Definition: ni_device_api.h:2209
NI_MIN_WIDTH
#define NI_MIN_WIDTH
Definition: ni_device_api.h:124
_ni_encoder_config_t::hdrEnableVUI
uint32_t hdrEnableVUI
Definition: ni_device_api_priv.h:531
ni_device_session_context_init
ni_retcode_t ni_device_session_context_init(ni_session_context_t *p_ctx)
Initialize already allocated session context to a known state.
Definition: ni_device_api.c:156
_ni_encoder_config_t::ui16iFrameSizeRatio
uint16_t ui16iFrameSizeRatio
Definition: ni_device_api_priv.h:599
_ni_encoder_cfg_params::preferred_transfer_characteristics
int preferred_transfer_characteristics
Definition: ni_device_api.h:2193
_ni_t408_config_t::intra_mb_refresh_arg
int32_t intra_mb_refresh_arg
Definition: ni_device_api_priv.h:492
_ni_session_context::pixel_format
int pixel_format
Definition: ni_device_api.h:1567
_ni_instance_mgr_general_status::active_hwupload_sub_inst_cnt
uint8_t active_hwupload_sub_inst_cnt
Definition: ni_device_api_priv.h:96
_ni_xcoder_params::use_low_delay_poc_type
int use_low_delay_poc_type
Definition: ni_device_api.h:2631
_ni_frame::flags
int flags
Definition: ni_device_api.h:2588
_ni_encoder_cfg_params::conf_win_top
int conf_win_top
Definition: ni_device_api.h:2225
ni_params_print
void ni_params_print(ni_xcoder_params_t *const p_encoder_params)
Print xcoder user configurations.
Definition: ni_device_api_priv.c:12204
_ni_encoder_config_t::ui8EnableAcqLimit
uint8_t ui8EnableAcqLimit
Definition: ni_device_api_priv.h:638
_ni_t408_config_t::nrInterWeightCr
uint32_t nrInterWeightCr
Definition: ni_device_api_priv.h:429
_ni_metadata_enc_bstream::reconChromaSize
uint32_t reconChromaSize
Definition: ni_device_api_priv.h:298
ni_ai_query_network_ready
ni_retcode_t ni_ai_query_network_ready(ni_session_context_t *p_ctx)
Definition: ni_device_api_priv.c:15196
_ni_session_context::sei_trailer
uint8_t sei_trailer[NI_CC_SEI_TRAILER_LEN]
Definition: ni_device_api.h:1373
_ni_encoder_config_t::ui8noMbtree
uint8_t ui8noMbtree
Definition: ni_device_api_priv.h:619
QUADRA
#define QUADRA
Definition: ni_defs.h:115
ni_ai_network_layer_size
uint32_t ni_ai_network_layer_size(ni_network_layer_params_t *p_param)
Definition: ni_util.c:3345
CHECK_VPU_RECOVERY
#define CHECK_VPU_RECOVERY(ret)
Definition: ni_device_api_priv.c:707
_ni_t408_config_t::chromaCbQpOffset
int32_t chromaCbQpOffset
Definition: ni_device_api_priv.h:414
_ni_metadata_enc_bstream::frame_type
uint32_t frame_type
Definition: ni_device_api_priv.h:276
STD_JPEG
@ STD_JPEG
Definition: ni_device_api_priv.h:360
_ni_nvme_identity::hw2_codec_type
uint8_t hw2_codec_type
Definition: ni_nvme.h:196
_ni_encoder_config_t::ui32VuiDataSizeBits
uint32_t ui32VuiDataSizeBits
Definition: ni_device_api_priv.h:532
_ni_encoder_cfg_params::bitrateMode
int bitrateMode
Definition: ni_device_api.h:2327
_ni_encoder_config_t::ui8inLoopDSRatio
uint8_t ui8inLoopDSRatio
Definition: ni_device_api_priv.h:576
_ni_nvme_identity::ai8Fr
uint8_t ai8Fr[8]
Definition: ni_nvme.h:97
_ni_metadata_dec_frame::sei_size
uint16_t sei_size
Definition: ni_device_api_priv.h:234
_ni_session_context::pkt_custom_sei_set
ni_custom_sei_set_t * pkt_custom_sei_set[NI_FIFO_SZ]
Definition: ni_device_api.h:1407
NI_PIXEL_PLANAR_FORMAT_TILED4X4
@ NI_PIXEL_PLANAR_FORMAT_TILED4X4
Definition: ni_device_api.h:889
NI_LOG_DEBUG
@ NI_LOG_DEBUG
Definition: ni_log.h:62
NI_CC_SEI_BYTE5
#define NI_CC_SEI_BYTE5
Definition: ni_device_api.h:436
_ni_metadata_enc_frame::force_pic_qp_enable
uint16_t force_pic_qp_enable
Definition: ni_device_api_priv.h:248
get_dev_pcie_addr
void get_dev_pcie_addr(char *device_name, char *pcie, char *domain, char *slot, char *dev, char *func)
_ni_nvme_identity::hw0_codec_type
uint8_t hw0_codec_type
Definition: ni_nvme.h:170
_ni_frame::crop_bottom
uint32_t crop_bottom
Definition: ni_device_api.h:2510
NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
#define NI_ENC_REPEAT_HEADERS_ALL_I_FRAMES
Definition: ni_device_api.h:1999
_ni_t408_config_t::pu04IntraAngleDeltaRate
int32_t pu04IntraAngleDeltaRate
Definition: ni_device_api_priv.h:457
_ni_xcoder_params::dec_input_params
ni_decoder_input_params_t dec_input_params
Definition: ni_device_api.h:2661
_ni_network_perf_metrics::total_cycles
uint32_t total_cycles
Definition: ni_device_api.h:1337
_ni_decoder_config_t::ui8EnableAdvancedEc
uint8_t ui8EnableAdvancedEc
Definition: ni_device_api_priv.h:719
STD_HEVC
@ STD_HEVC
Definition: ni_device_api_priv.h:355
_ni_metadata_common::frame_type
uint16_t frame_type
Definition: ni_device_api_priv.h:220
_ni_metadata_dec_frame::first_sei_header
ni_sei_header_t first_sei_header
Definition: ni_device_api_priv.h:231
STD_AVC
@ STD_AVC
Definition: ni_device_api_priv.h:344
_ni_sei_header
Definition: ni_device_api.h:331
_ni_nvme_identity::fw_branch_name
uint8_t fw_branch_name[256]
Definition: ni_nvme.h:218
_ni_nvme_identity::fw_commit_time
uint8_t fw_commit_time[26]
Definition: ni_nvme.h:219
_ni_gop_params::temporal_id
int temporal_id
Definition: ni_device_api.h:1906
_ni_global_session_stats
Definition: ni_device_api_priv.h:148
ni_get_log_lba
uint32_t ni_get_log_lba(ni_core_type_t eCoreType)
Definition: ni_device_api_priv.c:16996
NI_PIXEL_PLANAR_FORMAT_PLANAR
@ NI_PIXEL_PLANAR_FORMAT_PLANAR
Definition: ni_device_api.h:888
_ni_encoder_config_t::ui8noHWMultiPassSupport
uint8_t ui8noHWMultiPassSupport
Definition: ni_device_api_priv.h:620
_ni_frame::p_custom_sei_set
ni_custom_sei_set_t * p_custom_sei_set
Definition: ni_device_api.h:2570
ni_encoder_session_sequence_change
ni_retcode_t ni_encoder_session_sequence_change(ni_session_context_t *p_ctx, ni_resolution_t *p_resolution)
Send sequnce change to a xcoder encoder instance.
Definition: ni_device_api_priv.c:4872
_ni_t408_config_t::pu08IntraAngleDeltaRate
int32_t pu08IntraAngleDeltaRate
Definition: ni_device_api_priv.h:460
_ni_packet::avg_frame_qp
uint32_t avg_frame_qp
Definition: ni_device_api.h:2737
ni_decoder_frame_buffer_free
ni_retcode_t ni_decoder_frame_buffer_free(ni_frame_t *p_frame)
Free decoder frame buffer that was previously allocated with ni_decoder_frame_buffer_alloc,...
Definition: ni_device_api.c:3574
_ni_encoder_frame_params::video_width
uint16_t video_width
Definition: ni_device_api_priv.h:201
_ni_session_context::roi_avg_qp
uint32_t roi_avg_qp
Definition: ni_device_api.h:1448
ni_decoder_session_close
ni_retcode_t ni_decoder_session_close(ni_session_context_t *p_ctx, int eos_recieved)
Close a xcoder decoder instance.
Definition: ni_device_api_priv.c:1533
_ni_encoder_config_t::ui8EnableAUD
uint8_t ui8EnableAUD
Definition: ni_device_api_priv.h:535
_ni_device_capability::device_is_xcoder
uint8_t device_is_xcoder
Definition: ni_device_api.h:1110
ni_frameclone_desc_t
struct _ni_frameclone_desc ni_frameclone_desc_t
_ni_session_context::force_low_delay_cnt
uint32_t force_low_delay_cnt
Definition: ni_device_api.h:1651
NI_MAX_RESOLUTION_AREA
#define NI_MAX_RESOLUTION_AREA
Definition: ni_device_api.h:102
INST_BUF_INFO_RW_READ_BY_AI
@ INST_BUF_INFO_RW_READ_BY_AI
Definition: ni_device_api_priv.h:55
NI_RETCODE_ERROR_RESOURCE_UNAVAILABLE
@ NI_RETCODE_ERROR_RESOURCE_UNAVAILABLE
Definition: ni_defs.h:427
NI_RETCODE_PARAM_ERROR_MN_QP
@ NI_RETCODE_PARAM_ERROR_MN_QP
Definition: ni_defs.h:452
_ni_session_context::last_frame_offset
uint64_t last_frame_offset
Definition: ni_device_api.h:1406
_ni_encoder_cfg_params::forced_header_enable
int forced_header_enable
Definition: ni_device_api.h:2186
_ni_hw_capability::max_number_of_contexts
uint8_t max_number_of_contexts
Definition: ni_device_api.h:1092
ni_get_bitdepth_factor_from_pixfmt
int ni_get_bitdepth_factor_from_pixfmt(int pix_fmt)
Grab bitdepth factor from NI_PIX_FMT.
Definition: ni_device_api_priv.c:14836
_ni_session_context::session_statistic
ni_session_statistic_t session_statistic
Definition: ni_device_api.h:1601
_ni_session_stats::ui32LastErrorStatus
uint32_t ui32LastErrorStatus
Definition: ni_device_api_priv.h:142
_ni_session_context::async_mode
int async_mode
Definition: ni_device_api.h:1614
IDENTIFY_DEVICE_R
#define IDENTIFY_DEVICE_R
Definition: ni_nvme.h:686
ni_assert
#define ni_assert(expression)
Definition: ni_defs.h:322
_ni_t408_config_t::pu08DeltaRate
int32_t pu08DeltaRate
Definition: ni_device_api_priv.h:452
_ni_encoder_frame_params
Definition: ni_device_api_priv.h:196
NI_CODEC_FORMAT_H265
@ NI_CODEC_FORMAT_H265
Definition: ni_device_api.h:879
_ni_encoder_config_t::i32forceBframeQpFactor
int32_t i32forceBframeQpFactor
Definition: ni_device_api_priv.h:636
_ni_load_query::active_hwuploaders
uint32_t active_hwuploaders
Definition: ni_device_api.h:1167
_ni_frame::use_long_term_ref
uint8_t use_long_term_ref
Definition: ni_device_api.h:2518
NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
@ NI_RETCODE_PARAM_ERROR_INTRA_PERIOD
Definition: ni_defs.h:438
_ni_encoder_config_t::ui8NewRCEnable
uint8_t ui8NewRCEnable
Definition: ni_device_api_priv.h:605
_ni_session_context::last_access_time
volatile uint64_t last_access_time
Definition: ni_device_api.h:1634
QUERY_INSTANCE_NL_SIZE_V2_R
#define QUERY_INSTANCE_NL_SIZE_V2_R(sid, instance)
Definition: ni_nvme.h:763
_ni_packet::data_len
uint32_t data_len
Definition: ni_device_api.h:2732